$_POST forgets value when script recalled with <a href

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kilolima
Forum Newbie
Posts: 4
Joined: Wed Feb 19, 2003 4:03 am

$_POST forgets value when script recalled with <a href

Post by kilolima »

Hello,

ok, the first script is the POST form. The second script sets up the $_POST values for use. Then, the user has the option to reload the script directly without going back to the form (basically the first script gets user input for a map, and the second script displays the map and allows the user to zoom in or out of the map). But what happens, is that the $_POST values are no longer set, and so of course the script crashes. Do _$POST values only work when they come directly from a POST form? I thought they were globals?

I did try a workaround, by making a hidden form value $from_form, and then after checking to see it false, load the variables from $_SESSIONs, but this seems kinda klunky.

Thanks,

Kilolima
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

$_POST is a superglobal, but the individual variables (ex: $_POST['myvar']) only exist when they come from a POST form. So when you recall the page using a hyperlink, the variables go away.

A workaround is to create a post form with a submit button and hidden variables instead of a link.

Code: Select all

<a href="<?=$_SERVER&#1111;'PHP_SELF']?>">Your current way of doing this</a>

<form action="<?=$_SERVER&#1111;'PHP_SELF']?>" method="post">
  <input type="hidden" name="myvar1" value="the value you get from the previous form">
  <input type="Submit" value="Click this instead of a link">
</form>
User avatar
kilolima
Forum Newbie
Posts: 4
Joined: Wed Feb 19, 2003 4:03 am

Post by kilolima »

Daven,


Thanks, that clears that up about $_GLOBAL...

I would prefer not to replace my <a href links with forms, mostly just because form buttons are so ugly (yes, I know they can be replaced by an image, but how well do the various browsers support this?). Is there no way to have global_vars off and still be able to keep the simple functionality of <a href=self.php?myvar=myvalue>?

I tried:

Code: Select all

echo "<a href=self.php?$_SESSION&#1111;myvar]=myvalue> clickme </a> ";
but this doesn't seem to work. neither does:

Code: Select all

<a href=list.php?<? echo "$_SESSION&#1111;myvar]";?>=myintvalue> clickme</a>
Actually, I realize that <a href links are critical to my web application becuase it is using a dynamic image map built via php. Can it be done or do I need to turn global_vars back on (and mabye go to https)?

thanks

kilolima
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

You can submit forms with links,

<a href="#" onclick="javascript: document.formname.submit();">Link</a>

something like that, my javascript is a little rusty.

Or you could just try to use $_GET?
User avatar
kilolima
Forum Newbie
Posts: 4
Joined: Wed Feb 19, 2003 4:03 am

Post by kilolima »

hey hob_goblin,

Thanks for the javascript tip! Although I usually try to avoid js at all costs, I think this may be a time when it's use is unavoidable. Anyway, this seems to work to allow multiple <a href to each submit different values using the $_POST global vars:

(first this script calls a include with session_start())

Code: Select all

<form method = post name = form2 action = test2.php>
<input type=hidden name=one value=0>
<a href="#" onclick="javascript:document.form2.one.value=10;javascript:document.form2.submit();">Link1</a>

<a href="#" onclick="javascript:document.form2.one.value=20;javascript:document.form2.submit();">Link2 </a>

<a href="#" onclick="javascript:document.form2.one.value=30;javascript:document.form2.submit();">Link3</a>
This should solve alot of issues and open the way to get around the no global_vars restrictions. Thanks!

kilolima

p.s. just in case, does anybody have a way to do this without using javascript?
User avatar
kilolima
Forum Newbie
Posts: 4
Joined: Wed Feb 19, 2003 4:03 am

Post by kilolima »

oh bother,

the above doesn't work in IE 5., just mozilla (Phoenix .5)...

now I remember why I hate javascript...
superwormy
Forum Commoner
Posts: 67
Joined: Fri Oct 04, 2002 9:25 am
Location: CT

Post by superwormy »

Use $_REQUEST instead of $_POST / $_GET.

$_REQUEST will use either the GET or POST variables, depending on which on eis set.
Post Reply