Strange issue with image submit not posting to PHP

I have an issue with a submit in a simple form not posting its value to a PHP page, when that submit is of type “image”. It works fine as a type “submit” but it needs to work as an image.

My form is just this:


<form action="archive_model.php" method="post" name="modelsearch">
<table width="600" class="cardisplay">
<tr>
<td width="200" valign="top"><input type="image" value="DB2" name="submit" src="images/car01.jpg" width="200" height="133" />
</td>
</tr>
</table>
</form>

If I take away the src="… and make it type “submit” it works fine.

Is it a simple syntax error in the input (which I don’t think it is) or something else? I’ve tried it in FF 3.5 and IE 8 and it doesn’t work in either browser.

Any help appreciated - thanks!

print_r($_POST);

different browsers are inconsistent in what they send to the server in regards to submit type buttons. Unless you feel like familiarizing yourself with all the details, you’ll get more robust code by only using the button as a submit mechanism. Make your serverside script work independent of any submit name/values.

You can use a hidden input instead if you need to pass a value.

So instead of type=“image” or type=“submit” use type=“hidden” and assign it the same value (DB2) as the input I was using previously?

Thing is I still need the image to display and have the PHP query run when a user clisk on it…

I’m not saying don’t use buttons to trigger the browsers form submission routine.
I am saying don’t put name/value pairs on the button itself. Use other elements instead.

An html form can contain as many input elements as you wish.

Okay, well I changed the form to this:


<form action="archive_model.php" method="post" name="modelsearch">
<table width="600" class="cardisplay">
<tr>
<td width="200" valign="top">
<input type="hidden" value="DB2" name="submit" />
<input type="image" src="images/logo.jpg" width="200" height="133" />
</td>
</tr>
</table></form>

But in the output of the PHP page it sends to, I just get this (and no actual results, nor does an echo $query on that page work)


Array
(
    [submit] => DB2
    [x] => 109
    [y] => 71
)

So even with a hidden input that has the required value, it still isn’t sending the value through…

This is what I have in the top of the PHP page that it tries to call:


<?php

echo '<pre>';
print_r($_POST);
echo '</pre>';

if($_POST['modelsearch'])
{

include ('inc/dbconnect.php');
  
// Get the search variable from form that was sent
  $modelref =@$_POST['submit'];
  echo $modelref;

etc…

But it isn’t echoing the $modelref…

Of course it’s sending the value. What do you think [submit] => DB2 is?

Your code however, does test for a variable that doesn’t exist. Don’t put names on forms.

Sorry but I’m confused by this. The name of the form is modelsearch, and I start the PHP page by checking that that form (with that name) has been sent.

It works fine when I just use an ordinary submit button.

If I shouldn’t even name the form, how can i get the PHP page to check that that form has indeed been sent?

Think I might have got somewhere, by changing the head of the PHP page:


if (isset($_POST['submit'])) 
{

include ('inc/dbconnect.php');
  
// Get the search variable from form that was sent
  $modelref =@$_POST['submit'];
  echo $modelref;

etc.

So basically testing for submission of the submit input rather than the form. I’ve still kept the name of the form however.

I’ll need to create a form now for each value e.g DB3, DB4 etc. but if this one works then so should they…

Hmm, spoke too soon as there are still a few issues:

I’ve changed the form so that it has two different submits (one for each of the different values, DB2 or DB4), and changed the PHP so that it firstly runs the test for submit, and then in a completely separate if, runs the test for submit2.

I’ve also echoed the mySQL queries and they look fine e.g


SELECT * FROM cars WHERE Model_type LIKE 'DB2' ORDER BY Chassis_no

And the same for DB4.

The form that sends to this page is now:


<form action="archive_model.php" method="post" name="modelsearch">
<table width="600" class="cardisplay">
<tr>
<td width="200" valign="top">
<input type="hidden" value="DB2" name="submit" />
<input type="image" src="images/archivegallery/db2.jpg" width="200" height="133" />
</td>
<td width="200" valign="top">
<input type="hidden" value="DB4" name="submit2" />
<input type="image" src="images/archivegallery/db4.jpg" width="200" height="133" />
</td>
</tr>
</table></form>

This kind of works in that if I click the first image (DB2) then I get the right results. If however I click the DB4 image, I get all the results for DB2 as well as the results for DB4.

Any way I can stop it bringing up the DB2 results for DB4? Maybe I need to have two completely separate forms then the queries can’t interfere with each other…

The form name is never sent to PHP only the form fields and values pairs.
Personally I always include a hidden form that has a reference number or name eg:


<input type="hidden" name="form_id" value="addProductForm" />

and then check for that in the POST array before doing any processing


if(isset($_POST['form_id'])) {
// process stuff
}

It helps to keep track of forms too

Remember that you have only got 1 form and so that is what is being submitted - no matter what button was clicked!

If you need to submit different values, use different forms

Thanks, as I suspected setting up different forms seems to work fine:


<table width="600" class="cardisplay">
<tr>
<td width="200" valign="top">
<form action="archive_model.php" method="post" name="modelsearch">
<input type="hidden" value="DB2" name="submit" />
<input type="image" src="images/archivegallery/db2.jpg" width="200" height="133" />
</form>
</td>
<td width="200" valign="top">
<form action="archive_model.php" method="post" name="modelsearch">
<input type="hidden" value="DB4" name="submit" />
<input type="image" src="images/archivegallery/db4.jpg" width="200" height="133" />
</form>
</td>
</tr>
</table>

Now to create all the others and that should hopefully be that. Thanks.

depending on how many forms you have and how adventurous you are feeling, you can do this with one form and multidimensional arrays…!