Forum Moderators: coopster
I have the following simple php code:
<?php
if ($submit)
{?>
Hi <?php echo $_POST["name"];?>.
<?}
else
{
?>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="POST">
Your name: <input type="text" name="name" />
<input type="Submit" name="submit">
</form>
<?php
}
?>
This code works very well in a free PHP web host.
But when I tried to run it in my PC (Windows 2000 + Apache 2.0 + PHP 4.3), only the form part was always executed. It seems that $sumbit can never be true, and variable value cannot be passed.
Later I tried in my another PC (Redhat 8.0 + Apache 2.0 + PHP 4.3), the result was the same.
I am very sure that the problem lies in PHP in my machine, maybe I didn't configure it correctly.
But I cannot figure out how it goes wrong?
Thanks for help!
when you view source on the page with the form what do you see as the action? I think it will be blank.
<?php $_SERVER['PHP_SELF'];?>
you would need to change this to either
<?php echo $_SERVER['PHP_SELF'];?>
or
<?= $_SERVER['PHP_SELF'];?>
which are both the same but without echoing the value of the var there should be no value for action.
To check whether the FORM [w3.org] has been posted check if
$_SERVER['REQUEST_METHOD'] == 'POST'. Andreas
The server I'm hosted on uses some funky mappings to redirect you to my site, so $PHP_SELF returns the "wrong" value (it points at where the file is on the server as opposed to the url you would have to go to to access that file), so it doesn't work for me.
I found that using action="" will cause the form to submit to "itself", i.e. the current page.
FYI, I use a hidden field to check for form submission:
<input type="hidden" name="submitted" value="true" />
<?php
if ($_POST['submitted'])
{
//process the form
}
else
{
echo("go away!");
}
?>
The W3C definition (HTML 4.01) doesn't say that...
Well, it entirely depends on what Andreas meant. It is entirely valid to use an empty action attribute.
[w3.org...] URIs are resolved to full URIs using a base URI. [RFC1808], section 3, defines the normative algorithm for this process. For more information about base URIs, please consult the section on base URIs in the chapter on links.
So, the action attribute has to be included, but if it's left empty it will be interpreted as a relative URI. In this case, it should point to the current directory, in which case the current document will only be opened if it is the default document for that directory.