<?php
if(isset($_POST['name']) && !empty($_POST['name'])) {
$name = htmlentities($_POST['name']);
$handle = fopen('names.html', 'a');
fwrite($handle, "<br />".$name);
fclose($handle);
} //if
?>
<h4>Add your name to the list!</h3>
<form action="simple.php" method="POST">
Your name:<br />
<input type="text" name="name" /><br />
<input type="submit" value="Add Your Name">
</form>
The script above works fine for adding names to a list in a html page.
I want to change the form action from “simple.php” to “names.html”, so that once someone submits the form they are brought to the html page with the list of names. However, when I change the form action to “names.html”, the name submitted in the form, is not added to the list of names in the html file. Why is that?
How can I alter the script so that when the form is submitted the name is added to the list and the user is brought to the names.html page.
I want to add to this answer that the most proper way in this case is to do a redirection with the 303 “See Other” http status code, which is just intended for the purpose of redirecting from a POST request - this way you will avoid some minor edge-case inconveniences in some browsers. Moreover, Location requires an absolute URL. The code therefore should be something like that:
What does it mean “in a simple structure”? Can you back up your claim? The HTTP specification clearly states that The field value consists of a single absolute URI.
A simple structure is: a no-rewrite interface (just simple.php, no rewrite engine) and a browser (not talking about console).
The www.w3.org directive is about general headers but the browser also accepts relative paths, that’s what I wanted to say with “not required”.
The browser knows “where he is” and will redirect relative to the current folder.
However, having an absoluteURL is a better practice but it’s not required.
In a simple script (like this - a simple form submit) you don’t have to use an absolute URL.
As an example of “not required”, this works in any browser (or, tell me one that fails):
It’s interesting, I’ve found that in the next version of HTTP standard relative URL are to be allowed. So just to be safe, I’ll wait till the new stadard becomes official for while.
Yes, browsers have no problem with relative URLs, in fact I believe they should be officially allowed just to make life easier.