Enj
Enj

Reputation: 23

save php output as a new html file

I worked out a form to fill out on a simple php site, how can I set up a script to put that information in a html/php file and finally save it on the server (file name should be the same name as the $name) $name = $email = $friendcode = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
   $name = test_input($_POST["name"]); 
   $email = test_input($_POST["email"]);
   $friendcode = test_input($_POST["friendcode"]);
}

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
   Name: <input type="text" name="name"><?php echo $nameErr;?>
   <br><br>
   E-mail: <input type="text" name="email"><?php echo $emailErr;?>
   <br><br>
   Friendcode: <input type="text" name="friendcode"><?php echo $friendcodeErr;?>
   <br><br>
   <input type="submit" name="submit" value="Submit">
</form>

Upvotes: 0

Views: 1742

Answers (2)

Try to create a file using fopen,

$save_str = '<p>Name :'.$name.'<br/>e-mail:'.$email.'<br/>friend code'.$friendcode.'</p>';

$handle = fopen($name.'.html','c');
fwrite ($handle, $save_str);
fclose ($handle);

more about fwrite

Upvotes: 1

user2291398
user2291398

Reputation:

You can use the fopen(), fwrite() and fclose() functions provided by PHP.

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
   $name = test_input($_POST["name"]); 
   $email = test_input($_POST["email"]);
   $friendcode = test_input($_POST["friendcode"]);

   $file=fopen($name,"a") or die("can't open file");
   fwrite($file,$email." ".$friendcode);
   fclose($file);
}

This will result in a simple text file, but you can also fwrite() any HTML/PHP code you like.

Upvotes: 1

Related Questions