Question re form action and fopen()

<?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.

Thanks!

Why don’t you just add a header(‘Location: names.html’); after the fclose($handle);

What code processes your form data in the file ‘names.html’

For php code to work, the file must have the extension ‘.php’
You can’t simply change a php file extension to html and expect to work.

Why do you need to do this anyway? Just echo your html in the php file.

Thank you vectorialpx, that works perfectly.

Thanks 2ndmouse, that helps me understand why it wouldn’t work.

I want to create a new html file for the list of names submitted from the form.

does this help - untested, but it should work

<?php

if(isset($_POST['name']) && !empty($_POST['name'])) {
$name = htmlentities($_POST['name']);

$handle = fopen('names.html', 'a');
fwrite($handle, "<br />".$name);
fclose($handle);
$file = file('names.html');
echo '<pre>';
print_r($file);
echo '</pre>';
} //if
else{
echo '
<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>';
?>

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:


header("Location: http://" .$_SERVER['HTTP_HOST']. "/names.html", true, 303);

In a simple structure, it does not require.
However, your 303 completion is a better practice.

One other small thing: after the header it’s recomended to add an exit;

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. :slight_smile:
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):

 <?php
if( isset($_GET['redirect']) ) {
	header('Location: ' . basename(__FILE__));
	exit;
}
echo 'Initial. <a href="' . basename(__FILE__) . '?redirect">Test redirect</a>';
?> 

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.

Thanks again everyone for your very helpful replies. I am just learning PHP at the moment and the header function is working fine for the moment.

Thanks for that tip, I’ve added it to my script. What is the advantage of using exit; like that?

After the header was send, the script will not stop. PHP just sends a header… but things are still working after your redirect. Example:


<?php
header('Location: http://www.google.com');
file_put_contents('file.txt', 'we have it');

so, just to be sure:


<?php
header('Location: http://www.google.com');
exit; // we stop here!
file_put_contents('file.txt', 'we have it');

Thanks for the explanation vectorialpx.