Save form inputs to a text file

Well, you are wrong. Header works in my 7.2. I suspect you spelled it incorrectly.
If it is on a live server, you might need to add the http: to the address. But, it works fine in 7.2.
It would be spelled like this:

header("Location: success.php");

But the code you gave me earlier (8/21 14 Nov) was:
file_put_contents("…/…/a/YourFileNameHere.txt", $text_data);
header(“success.php”);
No mention of Location: in the header(“success.php”)

Sorry, either I mistyped it or copied and pasted it wrong. Sorry for that!

The redirect upon successful completion of post method form processing code should be to the exact same url of the current page. This will cause a get request for that page, which will prevent the browser from trying to re-submit the form data if the page gets reloaded or browsed away from and back to.

The redirect code goes inside the post method form processing code, which should first detect if a post method from has been submitted. Your current form page always redirects.

Any redirecting you do needs a php exit; statement after it to stop php code execution.

Generally, the lack of a failure message indicates success. If you want to display a one-time success message, store it in a session variable, then on each page load, test, display, and clear the session variable at the appropriate location in the html document. If you want the visitor to be able to go to a different page after that, provide navigation links to do so.

As to the various html markup mistakes, you should always validate the resulting html of a page at validator.w3.org

1 Like

I only use forms to collect homework.

Before I learned to mark the homework directly using PHP/MySQL, I used to collect the form data as text, download the text files once a week and mark them using Python.

For writing the form data to text I used this:

$body = "
Studentnr = ".$studentnr."
".$q1."
".$q2."
".$q3."
".$q4."
";

$path = "/home/mywebpage/public_html/19BEhw/php/uploads/";
$time = microtime($get_as_float = false);
$newname = $studentnr . "_19BEsW10_" . $time;
$fp = fopen($path . $newname, 'w');
fwrite($fp, $body);
fclose($fp);
//echo ' 成功! Your data has been saved 1 time in a text file! <br><br> ';

Nowadays, PHP marks the answers and writes them all to a table! Progress!

Sponsor our Newsletter | Privacy Policy | Terms of Service