4WebHelp: Troubles with form variables

OK, so you're pulling your hair out, because you just setup a very simple form which POSTs to a PHP script, but it doesn't seem to work. This is the code you're using:

<form name="test" action="script.php" method="post">
Your Name: <input name="name" value="" size="5" />

print('Your name is '.$name);

Theoretically, that should work, as long as your form field is called name. Most likely, if that's not working, you're using a recent version of PHP (4.2 and above) with a certain setting set to off: register_globals. This pesky little setting, which can be changed in the php.ini file (if you're on a shared hosting server you probably won't have access to it), should never have been set to on in the first place. Everyone got used to assuming that if you submit a form with a field called foo in it, then the variable $foo will automatically be created. But things should never have been done that way. Unfortunately it's too late to change that. All I can do is educate you to stop assuming.

To find out what version of PHP you're using and if register_globals is on or off, simply create a phpinfo() page. At the top of the page will be the PHP version number, and under "Configuration - PHP Core" you will find the setting of register_globals.

By default, in PHP 4.2 and above, the register_globals setting is off. However, before that version, it always used to be set to on. In practice, what this means is that if a form field is called foo, $foo will no longer be automatically created. This may seem a terrible shame, but it was turned off for a reason: it can lead to terrible security problems if used lazily. The PHP Group decided to turn it off by default for your security. So, you're saying, What am I supposed to do? Don't worry, there is another way to get your form fields' values: $_GET or $_POST. These magical variables will contain all the data which was sent to the PHP script via GET or POST. If you don't know how your data is going to be submitted, then there is a variable which contains both of these together: $_REQUEST.

Now, let's give you an example. Let's say you're using the same form as above to send someone's name to a PHP script. Here's what you would have:

print('Your name is '.$_POST['name']);

Now that wasn't so difficult was it? Here's what you would do if you changed the form's method to GET:

print('Your name is '.$_GET['name']);

Here's one last one, to be used if you're not sure whether the form has been submitted using GET or POST (for example, if you want to allow people to either use a form or just enter script.php?name=Daniel in the address bar):

print('Your name is '.$_REQUEST['name']);

There are some other variables like $_GET, $_POST and $_REQUEST in PHP: $_COOKIE and $_SESSION. These would be used if the data is stored in a cookie, or if the data was stored using a PHP session. Here's an example:

setcookie ('YourName', 'Daniel');

print('Your name is '.$_COOKIE['YourName']);

In this example, you would call up the first script in your browser, and it would create a cookie called YourName. This cookie would contain the text "Daniel". Then you would call up the second script, which would output "Your name is Daniel".

© 4WebHelp and Daniel

Latest comments on this tutorial
here is a hard one,  I have a form that is dynamicly generated and has a set of variables that is dynamicly named.  some times there maybe one some times 50 and the names will be different each time. on submit when the form hadler gets the form I need to get all the variable names and varibles they contain.  

Any help would be great
mr t
boom!  thanks again.  confusing common problem, that had me stumped!
if register_globals is set to off in php.ini,with out changing that file,how to turn on the globla variables
       otherwise i am getting the this type of error pls hlep me.


Warning: Unknown(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0

How do you make a form field required?
Camille Richards
Thank you for your article.   I have been going crazy trying to figure this out and thanks for putting it in terms that us newbies and understand.
Thank you very much for explaining this and posting it here. I am a newbie, and suffered terrible pains with this one. 2 days wasted looking for the solution in the installation of PHP5 and MySQL. Thanks again.
Peter Vagner
Hi All
I have found this tutorial helpfull but I would extend it.
I have a simple trick to display all the variables with their names posted. I have used post method while sending form but you should change it to your needs.
Look at this code:
$keys =array_keys($_POST); // store all variable names into a numericaly indexed array begining at 0
for ($i =0; $i <count($keys); $i++) // go trought the created array and do....
echo $keys[$i] . ": " . $_POST[$keys[$i]] . "<br>\n"; // print data in the format var_name: var_value
here is a hard one,  I have a form that is dynamicly generated and has a set of variables that is dynamicly named.  some times there maybe one some times 50 and the names will be different each time. on submit when the form hadler gets the form I need to get all the variable names and varibles they contain.  

Any help would be great
I was working a php form tutorial on another site, but couldn't make it work.  Thanks for the code that supports later versions of php.

print('Your name is '.$_POST['name']);

This seems to work great, but I can't get this variable to be carried onto the next page:

$stamp = strtotime ("now");
$orderid = "$stamp-$REMOTE_ADDR";
$orderid = str_replace(".", "", "$orderid");

This is a unique order ID I want to create, then use it on the next page. But when I try to print it out, I get nothing.

print('Your ID is '.$_REQUEST['orderid']);

Any ideas?
Daniel, 4WebHelp Team Member
BARTOLOMEW HUNTINGDON SMYTHE: Yes, this applies to all variables.
Miguel Mézquita
How can I put THANKS in bigger size?
Your tutorial really help me!
Thanks again.
Daniel, 4WebHelp Team Member
You're print()ing out the data which you haven't collected yet! You have to add a submit button, and then print() out the userid on the page you're sending to, in this case script.php.
Boy, I'm one of the unlucky few for whom your tutorial doesn't work.  What am I missing?  
Here's my code:

<form name="test" action="script.php" method="post">
Your Name: <input name="userid" value="leo" size="5" />

print('Your userid is '.$_POST['userid']);

The output is "Your userid is     "  Shouldn't it say, "Your userid is leo"?
Daniel, 4WebHelp Team Member
I think:


should be:


Tell me if it works Smile.
Ram Kelath
Thank you so much.  I'm working on my very first php page using 4.3 on the Mac. Being a Cold Fusion developer, I was intrigued by the fact that you didn't qualify variables with a prefix (such as url.name or form.name) - the name as defined in the form field was the name you used in the action page.  But of course it didn't work when I tried it and I looked at my simple form and action page for hours until I found this post.
Thank you.  Now to find my php.ini file and fix it.

Add a new comment

Page URL: http://www.4webhelp.net/tutorials/php/register_globals.php
Back to the "pretty" page!

© 2024, 4WebHelp Team.