0

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>

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

better option than my answer please use this.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.