Page 1 of 1

mysterious disappearing variable using $_GET

Posted: Tue Nov 16, 2010 4:31 pm
by wtc
Pounding my head on wall for last 3 hours, figure I better get some hints from experts... :banghead:

I use $_GET method to obtain some IDs I need in my program. Generally working fine.

In this case, I use this: $last_cat = $_GET['category_id']; at the top of my php file. I can check this value upon starting the file - it is perfect.

The page itself is mostly an HTML form. I can check the value of the variable $last_cat at the end of the form (once it is displayed on screen), and it shows the value perfectly. So - going into the form, the value is legit.

However, upon saving the form, the value of $last_cat goes blank - it should be a 1, 2, 3 or 4, and suddenly it is nothing.

I call the save button like this: <input type="submit" class="bottom" value="save" /> Pretty standard stuff. Following clicking the Save button, there is some more code that initiates the actual data validation and saving routings. It is here I can test for the value and see that is it GONE.

if ($_SERVER['REQUEST_METHOD']=='POST') {
...do some validation things...
...save the data in the form...
...send the use to another page using: header("Location:somefile.php?attach_id=".$data_obj->att_id."&category_id=".$last_cat);
}

If I check for the value of $last_cat inside this "if" statement, using a simple Echo $last_cat; and Exit; function, I can see the value is gone. Obviously, I put this mini-debug check BEFORE the last line where it sends the user to another page. Indeed, the "header(...)" function works fine for the attach_id value (which comes from a value in a table, not a variable), but not for the category_id.

The problem is not in the "header" statement/function per se. As mentioned, the value of $last_cat is gone apparently as soon as I click on the "Save" HTML button or as soon as the program enters the "if" statement. I don't know why this is.

is there something happening upon submitting a form that I need to know? Any workaround if so? Do I need to create a "hidden" html line to temporarily store the value in $last_cat? If so, how would I do that (I've tried but no apparent success).

Thanks so much,

TC

Re: mysterious disappearing variable using $_GET

Posted: Tue Nov 16, 2010 7:05 pm
by califdon
wtc wrote:is there something happening upon submitting a form that I need to know? Any workaround if so? Do I need to create a "hidden" html line to temporarily store the value in $last_cat?
Yes, when you click on a "submit" button of a form, you are sending a new request to the server, which may be, in your case, the same script (that depends on the value of the "action" parameter in the <form> tag, which you haven't shown us). That's the nature of web page actions, each call to the server is a new request. Values of variables are never carried over to another script, except by using either the $_GET or $_POST methods So if you need to carry over a value to the next script, you must either pass that value in the URL string (the "get" method) or as the value in an <input> element within the form (hidden, if not desired to be displayed).

Re: mysterious disappearing variable using $_GET

Posted: Tue Nov 16, 2010 11:24 pm
by wtc
Thank you Califdon - while I don't necessarily know the solution yet, at least I understand why the variable disappears, thanks to your response, and that I am not completely crazy. Just mostly.

Here is the <form> tag: <form method="post" action="<?$_SERVER['PHP_SELF']?>">

I have the following line also in my form, but I bet I am not doing it correctly, or am missing a key step:

<input type="hidden" name="category_id" value=<?=$last_cat?> />

So...I have the $last_cat value just prior to sending the request to the server, I still need it to be available after the Submit button is clicked, so that the function "header(...)" will pass the Category_id to the next script. I am clearly missing at least one more step/instruction to make this happen. Would it be possible to get an example snippet of code I could check out?

thanks,

tc

Re: mysterious disappearing variable using $_GET

Posted: Tue Nov 16, 2010 11:32 pm
by requinix
Stuff in the URL is like using a <form> with method=get. Hence the variable $_GET.
However your form has method=post. That means you need to use $_POST to access it.

However there's also $_REQUEST: a combination of both $_GET and $_POST. If you use that (and if it's enabled, which it very likely is) instead then you should be good. Otherwise you can check the $_SERVER["REQUEST_METHOD"] against "GET" or "POST" to decide where you should look for your ID.

Re: mysterious disappearing variable using $_GET

Posted: Wed Nov 17, 2010 12:18 am
by wtc
Thank you - that info solved it!

I inserted this line AFTER the save when the script is called again: $cat_id = $_POST['category_id'];

This did the trick - the value is "back" in the script.

One last question so that I can understand what happened here. I added this line more by guess than knowledge:

<input type="hidden" name="category_id" value=<?=$last_cat?> />

But it was the key, right? It is required to send the value along with the new request to the server, and therefore be available to get by the script again, right?

thanks all, VERY much appreciated, I can stop the headbanging... :banghead: