...

View Full Version : Strange issue with image submit not posting to PHP



galahad3
02-17-2010, 05:54 PM
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!

Fou-Lu
02-17-2010, 06:08 PM
I know there is an article somewhere in the api about this, but I'm afraid I cannot find it.
What I can tell you with 100% certainty, is that the 'submit' will no longer exist. It will be under $_POST['submit_x'] instead. I don't believe that will apply to anything but the image input type though (so any other fields I believe are still correct).

galahad3
02-17-2010, 06:29 PM
Okay... so if it no longer exists (or exists under _POST x) (???) then how can I get it to work?

On the face of it it seems such a simple task to perform, I'm surprised it should be this involved...

mOrloff
02-17-2010, 06:37 PM
In these situations, I frequently print the array to see what exactly I'm getting back.
It usually clears things up significantly.
Try:


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

/* or */

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


That may show you what you should be looking for.

~ Mo

galahad3
02-17-2010, 06:45 PM
I echoed right at the top of the PHP file that the form submits to- and got a very strange looking output:



Array
(
[submit_x] => 110
[submit_y] => 67
)



Not sure what this means and what I can do about it. Presumably these are image co-ordinates but how will they help submit the form properly using the image?

Fou-Lu
02-17-2010, 07:07 PM
That I assume to also be the case, probably the top left and bottom right. Couldn't be certain though, but we don't really care about that.
I assume that you're current form probably looks something like this:


if (isset($_POST['submit']))
{
// do stuff
}

with the image, you'll need to modify that:


if (isset($_POST['submit_x']))
{
// do stuff
}


If my suspicions are right, if you had a text entry for say 'username', I believe its still just under username. So you can look at switching based on a different field so you can update you're submit buttons to images without damaging the underlying code.

mOrloff
02-17-2010, 09:28 PM
... Presumably these are image co-ordinates ...

That I assume to also be the case, probably the top left and bottom right. Couldn't be certain though, ...

I realize this is besides the point, but I'm pretty sure that those are simply the single-point X,Y coordinates of the actual click, rather than the top-left & bottom-right coord's of the image.

:)
~ Mo

Fou-Lu
02-17-2010, 09:41 PM
I realize this is besides the point, but I'm pretty sure that those are simply the single-point X,Y coordinates of the actual click, rather than the top-left & bottom-right coord's of the image.

:)
~ Mo

That could be the case too, though I'm not sure if the browser would indicate a click point. I can test that when I get home, but I can't do that at work I'm afraid.

mOrloff
02-17-2010, 09:55 PM
Yeah, if you test it, please post your findings. (I can't test right now either)
I thought I remembered reading something about that a fair while back on 3wschools, but after a preliminary search, I'm not seeing it now.

mOrloff
02-17-2010, 10:27 PM
To Galahad: I'm not sure how fresh this all is to you or not, so I'm going to lay down some advice just in case.

I see that you're putting your form in a table.
If your primary reason for using a table is layout, please look into CSS.

If you are learning HTML right now, may as well learn the right way to do it.
Even if you're already comfortable with tables, css is not very difficult to step into (and even when hand-coding, some IDE's provide enough suggestions to almost do the work for you).

Tables (when used for non-tabular data) make everyones lives more difficult, even yours.
Any time that you save up-front will doubtlessly get spent some time down the road when (not if :)) you revisit your code, and the user has less influence on their own experience.
Inferior user experience translates into inferior traffic.

~ Mo

galahad3
02-18-2010, 12:39 PM
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'll need to create a form now for each value e.g DB3, DB4 etc. but if this one works then so should they...

DaiWelsh
02-18-2010, 12:50 PM
I can confirm that an image will not submit the value, the solution I would suggest is to rename the buttons to include the value so in your example you would rename to "submit_db2" or similar, then either:

- loop through the $_POST array checking anything that starts with 'submit_' and splitting off the end to get the value

or

- have a hidden field that contains a list of the possible values and loop through that looking for a suitably named $_POST variable

or

- reuse whatever query you originally used to decide what values to check for.


The first option is probably simplest provided you ensure you never name another field with the prefix 'submit_'

Another possible gotcha is invalid field names, but then that would be solved if you used a unique id number rather than a model name which is better practice anyway.

HTH

Dai

galahad3
02-18-2010, 01:17 PM
Thanks, also as I suspected setting up different forms for each submit 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.

DaiWelsh
02-18-2010, 01:19 PM
Yep that should work too. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum