user1707973
user1707973

Reputation: 21

$_POST Variable Detected in Chrome but not in Firefox

I am using 2 images in a form to sort out query results from the database. The form is submitted using the POST method. When i click on the first image, the query results have to be sorted in ascending order, and when i click on the second, the results have to be sorted in the descending order. This is the code for the form:

 <form name="" action="" method="post">
   <input type="hidden" name="typep" value="price" />
   <input type="image"  name="sort"  value="asc"  src="images/asc-ar.png" />
   <input type="image"  name="sort"  value="desc" src="images/dsc-ar.png" />
 </form>

Now this is the code for checking if the $_REQUEST['sort'] variable is set and therefore whether sorting is required or not.

if ($_REQUEST['sort'] != "")
{
    $sort  = $_REQUEST['sort'];
    $typep = $_REQUEST['typep'];
   //query to be executed depending on values of $sort and $typep
}

Firefox does detect the $_REQUEST['typep'] variable but not the $_REQUEST['sort'] one. This works perfectly in Chrome though. When i test the site in Firefox, it doesn't detect the $_REQUEST['sort'] variable and therefore the if condition evaluates to false and the search results don't get sorted.

Upvotes: 2

Views: 1300

Answers (3)

rkosegi
rkosegi

Reputation: 14658

If you have multiple input controls with same name in form , they will appears as array in PHP not as single value.

What does

var_dump($_POST)

or

var_dump($_REQUEST)

show?

Upvotes: 0

Arun Killu
Arun Killu

Reputation: 14233

Actually Firefox will only send the coordinates of the click. If you want to use input type="image" you can use:

<input type="submit" name="sort" value="desc"><img src="..." /></input>

and

<input type="submit" name="sort" value="asc"><img src="..." /></input>

or you can use css to input type="submit". input type="image" is equivalent to giving background image to button, so use that instead.

Upvotes: 0

lanzz
lanzz

Reputation: 43168

Apparently at some point in the HTML5 standard development somebody decided that the actual value of input type="image" is unimportant, and they decided to just not require browsers to submit it in any way: link to specification.

Unfortunately, it seems not only Firefox sticks strictly to the specification. A comment to a bug submitted to the Firefox developers states that this behavior is also observed in Opera and Internet Explorer.

So, you can test if the image has been clicked by inspecting the .x/.y coordinates submitted with the form, but you cannot determine which of the two images has been clicked, because you don't reliably receive its value across different browsers, and browsers that still pass the value will likely also follow the spec and drop it at some point in the future. You will need to name the two buttons differently.

Upvotes: 1

Related Questions