开发者

Persist GET vars that created outside and inside a form

Lets say that I have an HTML page "myPage.php" with a form that uses the GET method and calls itself:

<form name="myForm" action="myPage.php" method="get">
 <input type="text" name="input1" />
 <input type="text" name="input2" />
 .
 .
 .
 <input type="submit" />
</form>

And also, in this page and outside the form, I have an anchor that also calls the page itself, but with a GET variable "myVar" added (a GET variable that is not one of the form's variables):

<a href="myPage.php?myVar=100"></a>

Now... I wish that all the variables will be persisted no matter whether 开发者_开发问答the form was submitted or the link was pressed.

e.g., if a user was pressing the link, the URL will get the 'myVar=100' + the form variables (as if the form was also submitted together with pressing the link), and vise versa - if the user submits the form, the URL gets the form's vars as well as the "myVar", if indeed it was in the URL.

Is there a way?

Thank you :)


Hence, the easy way is to set the links with

<a href="#" onClick="document.form_name.submit();">Something</a>

also, change the action in your form to

<form action"myPage.php?var=100" method="get" name="form_name" id="form_name">
...

and they will send the same form. Anyhow, you'll deal with form inputs one way or another, right?


EDIT: Well, if it depends on the link clicking, then:

1.) Receive the myVar in PHP

$myCurrval = $GET['myVar'];

2.) Assign it to a JS var

<script type="text/javascript">
var JOHNNY = <?php echo $myCurrval; ?>
</script>

2.) Add a hidden input

<input type="hidden" id="myVar" name="myVar" value="" />

2.) And change the link to

<a href="#" onClick="processForm();">Something</a>

3.) Then, create a JS method called, yeah, processForm(), who will decide if sets the myVal or not.

function processForm() {
 if (JOHNNY != ''){
   document.getElementById('myVar').value=JOHNNY;
 }
}

This way, your myVar value will propagate only if it was received before (it means, the first time you send it, it will persist). Somehow, I would use $_SESSION to keep things between requests! In fact, it is safer and lot easier!!! :)

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜