Forum Moderators: coopster

Message Too Old, No Replies

problem: PHP posting form to itself

It doesn't work in my PC

         

yf_wang

7:42 pm on Apr 12, 2003 (gmt 0)

10+ Year Member



I happen to come here when I am seeking answers to my problem. And I find the forum will be helpful.

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!

DrDoc

7:47 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World!

Well, $submit isn't necessarily true. Give your submit button a value:

<input type="Submit" name="submit" value="Submit">

The other thing is... Do you have register_globals on? Otherwise you need to use $_POST["submit"] instead of $submit

jatar_k

7:54 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld yf_wang,

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.

DrDoc

8:13 pm on Apr 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well spotted jatar_k! ;)

yf_wang

3:04 am on Apr 13, 2003 (gmt 0)

10+ Year Member



Thank you DrDoc jatar_k.

Following your suggestions, I figured it out my mistake.

The critical point is that I should have used $_POST["submit"] instead of $submit.

Thanks again.

andreasfriedrich

11:49 am on Apr 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is really no need to use the FORM [w3.org] elementīs action [w3.org] attribute if you want the data to be posted to the resource containing the form since this is the default behaviour in case no action [w3.org] URL has been specified. That is why the code worked despite the missing echo [php.net].

To check whether the FORM [w3.org] has been posted check if

$_SERVER['REQUEST_METHOD'] == 'POST'
.

Andreas

Allen

8:14 am on Apr 14, 2003 (gmt 0)

10+ Year Member



Andreas,

The W3C definition (HTML 4.01) doesn't say that, so I would always include it. Plus it wouldn't hurt t oaccount for any browsers that don't act in that manner.

Allen

DrDoc

10:18 am on Apr 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Andreas is still right in his explanation why it worked :)

But, yes, some versions of IE and NN default to the current directory instead of the current document (just like they do if you leave out the URL for <a href>)

andreasfriedrich

10:20 am on Apr 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Indeed, the action attribute is #REQUIRED in the DTD and needs to be a %URI.

Andreas

insin

10:31 am on Apr 14, 2003 (gmt 0)

10+ Year Member



Just to add my 2 $SMALL_CURRENCY_UNITs

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!");
}
?>

DrDoc

10:32 am on Apr 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

DrDoc

10:34 am on Apr 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



funky mappings to redirect you to my site

Can't you just put the file name in there manually?

insin

10:45 am on Apr 14, 2003 (gmt 0)

10+ Year Member



Can't you just put the file name in there manually?

Actually, I really should, as most of my forms appear on one page only. I'll put that one down to laziness ;-)

However, I first used this for a chatbox which was on every page, so putting the filename in manually wan't an option.

DrDoc

10:46 am on Apr 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then, wouldn't $_SERVER['REQUEST_URI'] work?

nosanity

3:36 pm on Apr 14, 2003 (gmt 0)

10+ Year Member



If you really want to use $submit, you may need to set it to $_POST['submit'] as register_globals is now by default, OFF.

noSanity