So I have been working on this for three days straight and just can't figure it out, maybe I just need some sleep but I finally got a working form and php code that saves contact form data to a txt file. The only problem is, when I try it, It only saves the message section, it does not save the Name and Email and Subject. If someone could help me out with this that would be awesome. I am relatively new to PHP sp bear with me. The html form I am using is: <html> <head> <title>Your Informations</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form action="test5.php?savedata=1" method="post"> Your Name: <input type="text" name="name"><br> Your Email:<br> <input type="text" name="email"><br> Your Subject:<br> <input type="text" name="Subject"><br> Your Message:<br> <textarea name="message" rows="5" cols="30"></textarea><br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> The PHP I am Using is: <?php $savedata = $_REQUEST['savedata']; if ($savedata == 2){ $data = $_POST['name']; $data = $_POST['email']; $data = $_POST['subject']; $data = $_POST['message']; $file = "YOURDATAFILE.txt"; $fp = fopen($file, "a") or die("Couldn't open $file for writing!"); fwrite($fp, $data) or die("Couldn't write values to file!"); fclose($fp); echo "Your Form has been Submitted!"; } ?>
Every time you assign something to $data with the = operator you erase the value $data held previously. Use the .= operator to append instead of overwriting. $data = $_POST['name']; $data .= $_POST['email']; $data .= $_POST['subject']; $data .= $_POST['message']; PHP:
Thank You So much ! That worked perfectly! My only question now is how to I separate the fields when they save. For example when it saves it saves like this lo how are you. I would like it to save like this: name hello how are you.
$data = $_POST['name']." ".$_POST['email']." ".$_POST['subject']." ".$_POST['message']; PHP: or $data = $_POST['name']."\n".$_POST['email']."\n".$_POST['subject']."\n".$_POST['message']; PHP:
thank you so much everyone for the help seriously the best help I have ever gotten on this stuff. Take care and happy new year!