Solutions for problems passing variables in PHP
Cannot pass variables in PHP? For some reason whatever you read in support forums don't work? Read this PHP tutorial for the solution!
Introduction to Passing Variables
There are 2 ways to pass variables in PHP: "GET" and "POST". You might recognize these from the form tag (<form method="GET">). The difference? GET passes the variables in the URL, and POST passes the variables hidden. Ever get the error "resubmit POST data" when you click back on your browser? This is POST in action: it is asking "do you want to send that hidden information again".
GET in PHP
You don't need a form to send variables in PHP. Simply use GET. The advantage to this is that you can save the data submitted to the page, because it is all in the URL. Just bookmarke the page. Here is how you do it:
That easy. var_ is the name of the variable, then equals, then the value you want to assign to that variable. You put a "?" before the first, then seperate the rest with "&".
Now, you can retrieve them in PHP. Usually, you can just enter this:
Which will produce "value"
If globals for PHP are not enabled (keep reading for more information), you need to add a line:
This is what people use for content management systems. The site URLs look like: http://www.domain.com/index.php?id=5 . The id=5 is read by a PHP script. Usually, this id matches a row in a MySQL database that has all of the information for that page.
POST in PHP
For PHP, POST data is usually sent from forms, so I'm going to go over how to retrieve your variables. First, you give a name to each of your <input name="varname"> tags. For each of these, you use this line of code to retrieve (where varname matches with the name of your input tag).
Do this for each input name, then you can use these variables in your script.
Globals
This is the most common problem. Most tutorials tell you that variables will automatically be available, but you can't use them. The variables are not being transfered in the URLs. The test? Type the line: echo $variable; (where variable is the name of the variable you are passing in the url). If nothing is displayed, then you need to use one of the following solutions.
Retrieve it in your code: This is the easiest, because you just add this line before you try to call the variable:
Thats it. You can use that variable now!
Enable Globals: Add this line to your .htaccess file (if you use apache). If you don't have this file, just make a blank file and call it .htaccess, then upload it to your server.
Thanks for reading, and I hope this helps. If you have any other solutions to common PHP problems, please submit a comment below!
Current Comments
17 comments so far (post your own)Turning register globals on is a very unsecure way of using web applications in php, espically for beginnners who will be taken advantage of. Consider spending the extra time to register them all through the shown methods above rather than turning register globals on. Most hosts no longer allow this.
Posted by Jordon (http://www.vipinteractive.net)on Thursday, 12.29.05 @ 02:15am | #766
Thanks for this simple yet useful tutorial! I was able to change the whole configuration of my website which had worked for 3 years in Reg. Globals = ON mode/; now it should be safer as it's OFF! :)
Posted by st31n on Friday, 10.27.06 @ 12:48am | #2639
I am having trouble with a passed variable. Was wondering if you could help. I am using the $_GET method as described above. But for some reason when I use it in the <head></head> section the correct value is not passed.
This, in essence is what is going on.
<head>
$styleType = $_GET['styleType']/;</head>
<body>
echo($styleType)/; // outputs BUSINESS
$styleType = $_GET['styleType']/;
echo($sytleType)/; // outputs COOL
</body>
If you go to the above URL you will see what is going on. Just selet ALL to see the output at the top of the page.
Thanks.
Posted by Tim (wwww.timbivans.com)on Friday, 12.22.06 @ 10:33am | #2687
$variable = $_GET["variable"]/;
resolved my problem. The tutorial that i was following didn/;t mentioned this and i wasted more of couple of hours troubleshooting.
Thanks lot !!!
Posted by Rahul SInha (simplyrahul@googlepages.com)on Saturday, 01.20.07 @ 05:17pm | #2723
Thanks a lot for the help. $_GET saved the day for me.
Posted by anirban on Saturday, 02.10.07 @ 11:30pm | #2760
Thanks for the tutorial. I have spent many hours wondering why my variable weren't passing. I knew the Register Globals is supposed to be off, but didn't know to set $variable to $_GET["variable"]
Posted by bb on Monday, 05.7.07 @ 09:20pm | #2869
This is really helpful, but i"m not sure what to do if the user hits the "back" button. When that happens the variable gets lost, of the page they are going back to is the one where my variable was originally set in the code, and not passed to it through the url.
Posted by Chris on Saturday, 05.12.07 @ 08:12pm | #2875
So can we use $_GET['variable'] even when registers_global is off with hyperlinks?
Posted by Animesh on Tuesday, 09.4.07 @ 09:23am | #3005
Hi! I'm having the following problem with PHP:
I want to pass a variable from one php page to another. I tried to use GET but it turned out that the value was too big (a string with many characters). As a result, the receiving page got an empty variable.
Is there a way to pass a variable from one php page to another regardless of its size?
Posted by Nick on Friday, 09.28.07 @ 11:28am | #3032
test
Posted by f (eee.eee.eee)on Thursday, 10.18.07 @ 05:54am | #3050
test
Posted by f (eee.eee.eee)on Thursday, 10.18.07 @ 05:54am | #3051
What if I want to pass a variable with a variable numeric value to a dynamic page? I've tried using GET and passing variable=$variable in the link but it's passing the name of the variable ($variable) instead of the numerical value. Anybody?????
Posted by Jill Torgerson on Friday, 01.4.08 @ 03:41am | #3140
Jill -
If you haven't gotten an answer somewhere else, here it is.
You need php code to replace the variable name with the value, something like this:
<a href="somewhere.com/index.php?variable=<?=$variable/;?>">
adding the code <?=$variable/;?> where you want the value of $variable to pass.
Hope it helps!
Mark
Posted by Markei on Saturday, 01.26.08 @ 10:09pm | #3176
Just a note: you don't need the forward slash in the code, it was put there by this comment system.
Posted by Markei on Saturday, 01.26.08 @ 10:11pm | #3177
Finally! After many hours of struggling with this I was able to get my forms to post to my PHP files.
I simply had to change the method="post" to method="get" in my <form> statement of my .html file and Voila! My variable now pass correctly to my action="myfile.php".
I tried over and over to get this to work, I can't believe how much time I spent for such an extremely simple problem. Thanks, great tutorial, I wish they were all laid out so simply.
Posted by Lectrikhead on Sunday, 01.27.08 @ 07:57am | #3178
I understand how to retrieve the variables through post but how do you set them in the calling script without requiring an HTML form or operator intervention?
Posted by Harry (http://reamerfoundation.orgfree.com)on Thursday, 01.31.08 @ 05:44am | #3182
This is a great tutorial for beginning PHP coders. Thanks for the contribution!
Posted by Dragolux (http://www.tutorialwow.com/)on Wednesday, 03.26.08 @ 08:17am | #3265
Rate this Tutorial
Current Rating: