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

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

LVL 1
Bjarte FjellandAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

szewkamCommented:
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
Bjarte FjellandAuthor Commented:
Thanks,
but could the HTML file all ready have content?
Like with logo and text?
0
Roger BaklundCommented:
>> 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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Bjarte FjellandAuthor Commented:
That is great, I'm gonna try that!
0
Bjarte FjellandAuthor Commented:
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
szewkamCommented:
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
Roger BaklundCommented:
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
Bjarte FjellandAuthor Commented:
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
Roger BaklundCommented:
>> 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
Bjarte FjellandAuthor Commented:
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
Roger BaklundCommented:
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
Bjarte FjellandAuthor Commented:
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
Roger BaklundCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bjarte FjellandAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.