Solved

Is there a PHP code that makes a form in to HTML file?

Posted on 2009-07-03
14
242 Views
Last Modified: 2013-11-17
Hi,

Is it possible to make a PHP code that sends the information from a html form to a html file?
The reason I'm asking is because our company want to change the email signature. And put in text and logo.
We use Lotus Notes 7.0.3 and the only choice I got now is to make a HTML file.
But my challenge is the users "signature".
The code I have so fare is:

If PHP is the hard way Im up for some Lotus script.
Thank you for your suggestion.

Fjebja
<FORM action="signature.html" method="get">

    <P>

    <LABEL for="fornavn">Fornavn:</LABEL>

              <INPUT type="text" id="Fornavn"><BR>

    <LABEL for="etternavn">Etternavn: </LABEL>

              <INPUT type="text" id="etternavn"><BR>

    <LABEL for="tittel">Jobb tittel: </LABEL>

              <INPUT type="text" id="tittel"><BR>

    <LABEL for="tel">Tlf: </LABEL>

              <INPUT type="text" id="tlf"><BR>

    <LABEL for="mobil">Mobil: </LABEL>

              <INPUT type="text" id="mobil"><BR>

    <LABEL for="email">email: </LABEL>

              <INPUT type="text" id="email"><BR>

    <BR>

    <INPUT type="submit" value="Send"> <INPUT type="reset">

    </P>

 </FORM>
 

PHP

****

<?

$content = $_POST['content'];

$file = &quot file.txt&quot ;

$Saved_File = fopen($file, 'w');

fwrite($Saved_File, $content);

fclose($Saved_File);

?>

Open in new window

0
Comment
Question by:Bjarte Fjelland
  • 7
  • 5
  • 2
14 Comments
 
LVL 7

Expert Comment

by:szewkam
ID: 24774621
you can't pass variables to html file. But without any problems you can generate html files from php script. Use file_put_contents (if you have PHP > 5) or combination of fopen, fwrite, fclose like you have in your script just change file.txt to file.html :)
0
 
LVL 1

Author Comment

by:Bjarte Fjelland
ID: 24774683
Thanks,
but could the HTML file all ready have content?
Like with logo and text?
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24774741
>> could the HTML file all ready have content?

No, you must write the entire file. But you can use a html file as a template, transposing the variables using placeholders. For example:

Template (template.tpl):

<div align="right"><img src="logo.gif" alt="Logo" /></div>
<div style="color:green;">{$Fornavn} {$etternavn}</div>
<div style="color:blue;">Tlf: {$tlf}</div>

...and in your PHP code:
$template=file_get_contents('template.tpl');

$html = str_replace(

  array('{$Fornavn}','{$etternavn}','{$tlf}'),

  array($Fornavn,$etternavn,$tlf),$template);

file_put_contents('signature.html',$html);

Open in new window

0
 
LVL 1

Author Comment

by:Bjarte Fjelland
ID: 24774787
That is great, I'm gonna try that!
0
 
LVL 1

Author Comment

by:Bjarte Fjelland
ID: 24820907
Sorry, I cant figure this out.
I made the template file, put the PHP code into a PHP file.
What should I do with my html file?

HTML file

****

<html>

<head>

   <title>Signature</title>

</head>
 

<body>
 

<FORM action="signature.html" method="PUT">

    <P>

    <LABEL for="fornavn">Fornavn:</LABEL>

              <INPUT type="text" id="Fornavn"><BR>

    <LABEL for="etternavn">Etternavn: </LABEL>

              <INPUT type="text" id="etternavn"><BR>

    <LABEL for="tittel">Jobb tittel: </LABEL>

              <INPUT type="text" id="tittel"><BR>

    <LABEL for="tel">Tlf: </LABEL>

              <INPUT type="text" id="tlf"><BR>

    <LABEL for="mobil">Mobil: </LABEL>

              <INPUT type="text" id="mobil"><BR>

    <LABEL for="email">email: </LABEL>

              <INPUT type="text" id="email"><BR>

    <BR>

    <INPUT type="submit" value="Send"> <INPUT type="reset">

    </P>

 </FORM>
 

</body>

</html>

****
 

PHP file

****
 

<?

$template=file_get_contents('template.tpl');

$html = str_replace(

  array('{$Fornavn}','{$etternavn}','{$tlf}'),

  array($Fornavn,$etternavn,$tlf),$template);

file_put_contents('signature.html',$html);
 

?>

****
 

Template file

****

<div style="color:green;">{$Fornavn} {$etternavn}</div>

<div style="color:blue;">Tlf: {$tlf}</div>

<div align="left"><img src="logo.jpg" alt="Logo" /><div style="color:green;">

****

Open in new window

0
 
LVL 7

Expert Comment

by:szewkam
ID: 24821118
the template file is your html before putting there right values.
So it should look like in snippet. And of course you have to change fields in template to what you need.
And after executing php script you get proper html file
//template.tpl

<html>

<head>

   <title>Signature</title>

</head>

 

<body>

<div style="color:green;">{$Fornavn} {$etternavn}</div>

<div style="color:blue;">Tlf: {$tlf}</div>

<div align="left"><img src="logo.jpg" alt="Logo" /><div style="color:green;">
 

<FORM action="signature.html" method="PUT">

    <P>

    <LABEL for="fornavn">Fornavn:</LABEL>

              <INPUT type="text" id="Fornavn"><BR>

    <LABEL for="etternavn">Etternavn: </LABEL>

              <INPUT type="text" id="etternavn"><BR>

    <LABEL for="tittel">Jobb tittel: </LABEL>

              <INPUT type="text" id="tittel"><BR>

    <LABEL for="tel">Tlf: </LABEL>

              <INPUT type="text" id="tlf"><BR>

    <LABEL for="mobil">Mobil: </LABEL>

              <INPUT type="text" id="mobil"><BR>

    <LABEL for="email">email: </LABEL>

              <INPUT type="text" id="email"><BR>

    <BR>

    <INPUT type="submit" value="Send"> <INPUT type="reset">

    </P>

 </FORM>

 

</body>

</html>
 

//php file

<?

$template=file_get_contents('template.tpl');

$html = str_replace(

  array('{$Fornavn}','{$etternavn}','{$tlf}'),

  array($Fornavn,$etternavn,$tlf),$template);

file_put_contents('signature.html',$html);

 

?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24821252
The action attribute in the form must contain the name of the PHP file, and the method should be "post".

<FORM action="signature.php" method="post">

The <input> fields in the form must have names:

    <LABEL for="fornavn">Fornavn:</LABEL>
              <INPUT type="text" id="fornavn" name="fornavn" /><BR>
    <LABEL for="etternavn">Etternavn: </LABEL>
              <INPUT type="text" id="etternavn" name="etternavn" /><BR>
    <LABEL for="tittel">Jobb tittel: </LABEL>
              <INPUT type="text" id="tittel" name="tittel" /><BR>
    <LABEL for="tel">Tlf: </LABEL>
              <INPUT type="text" id="tlf" name="tlf" /><BR>
    <LABEL for="mobil">Mobil: </LABEL>
              <INPUT type="text" id="mobil" name="mobil" /><BR>
    <LABEL for="email">email: </LABEL>
              <INPUT type="text" id="email" name="email"><BR>

In the PHP file, you must fetch the variables from the form using the $_POST array.
<?php
 

$fornavn = $_POST['fornavn'];

$etternavn = $_POST['etternavn'];

$tittel = $_POST['tittel'];

$tlf = $_POST['tlf'];

$mobil = $_POST['mobil'];

$email = $_POST['email'];
 

$template=file_get_contents('template.tpl');

$html = str_replace(

  array('{$fornavn}','{$etternavn}','{$tittel}','{$tlf}','{$mobil}','{$email}'),

  array(  $fornavn,    $etternavn,    $tittel,    $tlf,    $mobil,    $email), $template);

file_put_contents('signature.html',$html);

 

?>

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:Bjarte Fjelland
ID: 24907782
Thank you for your suggetions!

Im almost there.

I have my htm, template and php files working. Puh !
but there is two things I can't get to work and that is to promt the user to save the file as Signature.html, the only thing he og she gonna do is to locate the folder we use.
Tryed              FILE_APPEND            but getting a nasty PHP Warning.
And the other thing is if a form is empty how can I make the line be blank in the signature.html?

thanks.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24924125
>> to promt the user to save the file as Signature.html

...not sure if I understand... the file is saved on the web server with the name "signature.html". If you want the user to control the name of the file, you must have a field for filename in the form:

  <LABEL for="filename">Filnavn: </LABEL>
              <INPUT type="text" id="filename" name="filename"><BR>

You could use the user name for filename:

$filename = str_replace(' ','_',"{$fornavn}_{$etternavn}.html");

>> if a form is empty how can I make the line be blank in the signature.html?

You mean if a form field is empty? You could have multiple templates, with/without tlf, with/without email and so on, an select the appropriate template based on the input. Or you could create the template dynamically, based on the input. The first is easier to maintain, the second is more dynamic, but requires php skills to modify.
0
 
LVL 1

Author Comment

by:Bjarte Fjelland
ID: 24951223
This code is perfect : $filename = str_replace(' ','_',"{$fornavn}_{$etternavn}.html");
Thank you!

Im skipping the part with multiple template.

In my template I have a tiny logo.
When user save the HTML file the logo don't follow onto local disk.

Here is my PHP code so fare.

After the signature.html is saved, is it possible to promt to save the logo as well?
Tryed to get it to work, but cant solve it.

<?

$fornavn = $_POST['fornavn'];

$etternavn = $_POST['etternavn'];

$tittel = $_POST['tittel'];

$tlf = $_POST['tlf'];

$mobil = $_POST['mobil'];

$fax = $_POST['fax'];
 

$filename = str_replace(' ','_',"{$fornavn}_{$etternavn}.html");
 

$template=file_get_contents('template.tpl');

$html = str_replace(

  array('{$fornavn}','{$etternavn}','{$tittel}','{$tlf}','{$mobil}','{$fax}'),

  array(  $fornavn,    $etternavn,    $tittel,    $tlf,    $mobil,    $fax),   $template);

file_put_contents ("$filename",$html );
 

$file="$filename";

header ("Content-type: application/octet-stream");

header ("Content-disposition: attachment; filename=".basename($file).";");

header("Content-Length: ".filesize($file);

header("Connection: Close");

readfile($file);

exit;
 

?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24953228
You can not download two files in the same request, unless you use a zip file or similar. You could serve the logo as a separate download, with similar code as lines 17-22. Save this as "logo_download.php" and link to it from the form like this:

<a href="logo_download.php" target="_blank">Download logo</a>
<?php

$filename = 'logo.jpg';

header('Content-Type: image/jpeg');

header('Content-Length: '.filesize($filename));

header('Content-Disposition: attachment;filename="'.$filename.'"');

readfile($filename);

?>

Open in new window

0
 
LVL 1

Author Comment

by:Bjarte Fjelland
ID: 25013850
That's great, I've done the logo download separatly - Thanks.

I have to go with multiple templates after all, because there can't be empty form fields in the signature.
I've made the templates but I can't figure out the ' if (empty($name)) ' php code, and if there is two empty form fields - what to do then?
First name and Last name has to be in place, but the fax and mobile can be blank. (even both can be blank)
How do I make the php code to pick the right template?

I have one template when all the fields are field inn.
One without fax
One without mobile
One without fax and mobile.

I appreciate your answers, they help me understand php more!

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 25014081
You have four templates, so there will be four conditions in the if-elseif-else test:
if(empty($mobil) and empty($fax)) $template = 'template_without_mobile_and_fax.tpl';

elseif(empty($mobil)) $template = 'template_without_mobile.tpl';

elseif(empty($fax)) $template = 'template_without_fax.tpl';

else $template = 'template_all_fields.tpl';

Open in new window

0
 
LVL 1

Author Comment

by:Bjarte Fjelland
ID: 25018515
That is super! It works even thought I ended up with 8 templates.
I hade to put the file_get_contents code, se code snippet in

Thanks a million!

if(empty($fax)and empty($mobil)) $template = file_get_contents ('template_fax_mobil.tpl');

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

IBM Notes offer Encryption feature using which the user can secure its NSF emails or entire database easily. In this section we will discuss about the process to Encrypt Incoming and Outgoing Mails in depth.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now