0

I have a form using the GET method and also I'm using the action method to also pass on some URL variables:

<form action="items.php?a=1&b=2" method="get">...</form>

In items.php I use $_REQUEST to parse the contents of a,b vars but unless I use POST as method in the form, the variables appear empty or non-existent.

I've read about the differences between POST, GET, REQUEST but I'm not sure how to tackle this issue. I need the form to use GET so that it caches the results and when a user selects a result and then clicks back in his browser, he returns to the result set.

6
  • Try putting these in hidden fields: <input type="hidden" name="a" value="1" /><input type="hidden" name="b" value="2" /> inside the form. Commented Nov 8, 2012 at 11:58
  • so youre sayin that if you use POST you cant get the value of "$_REQUEST["a"]"? Sounds kinda strange to me.. Maybe you can try adding hidden input fields to store a and b variables in the form. Then use "items.php" as action. You should be able to receive all Input fields in $_REQUEST. edit: @jensgram: same thought same time Commented Nov 8, 2012 at 11:59
  • @jensgram: They need to be URL params. Commented Nov 8, 2012 at 12:29
  • @crothass: The form has to use GET, not POST. If I use GET to submit it, I cant retrive a&b with REQUEST. I need the parsing to be via REQUEST because the variables might be sent to this page in other parts of the application as well using other methods as well, ie in a url. Commented Nov 8, 2012 at 12:33
  • If I use POST then I then get a resultset in items.php, I click on one of them and then click back in my browser. Result: "Confirm Form Resubmission" error which doesnt occur when using GET. Whats the workaround? Commented Nov 8, 2012 at 12:36

5 Answers 5

6

First of all, form will delete variables a and b from the request. To add them to the GET-request, use something like this:

<form action="index.php" method="get" >
<input type="hidden" name="a" value="1" />
<input type="hidden" name="b" value="2" />
....
</form>

From php you can access these variables using $_GET, eg:

$_GET['a'] // 1
Sign up to request clarification or add additional context in comments.

Comments

2

Switch the Method to POST.

Now you will be able to retrieve BOTH.

-> Find the queryparameters via $_GET.

-> Find the POST parameters via $_POST

And do yourself a favor and never look back at that monstrosity $_REQUEST.

2 Comments

Ok, lets say I stick with POST. I then get a resultset in items.php, I click on one of them and then click back in my browser. Result: "Confirm Form Resubmission" error. Whats the workaround?
There are several. But first: Make sure you understand WHY the browser asks you that question: It is for a VERY legitimate reason bugging you. Compare it to this situation: (1) You walk into a bank. (2) You write a check for $100,- to your landlord. (3) Bank employee says to you: Thank you! (4) You walk out. Now suppose you could hit the back-button a few times back to (3). In that situation you are happy if the employee asked you: Are you sure you want to write a check of $100,- again? But the "solution" is called POST/REDIRECT/GET. Read here: en.wikipedia.org/wiki/Post/Redirect/Get
1

I think you should use $_GET instead of $_REQUEST.

For example:

$myVar = $_GET['a'];
$myVar2 = $_GET['b'];

Comments

1

To see the output vars of your $_GET form:

print_r($_GET);

And just use the following to access the vars:

$a = $_GET['a'];
$b = $_GET['b'];

Comments

1

use this in your from.... the used contents of a,b vars as hidden field

 <form action="items.php" method="get">
  ...
 <input type="hidden" name="a" value="1" />
 <input type="hidden" name="b" value="2" />
</form>

and get the variable as $_GET['a'] , $_GET['b']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.