I have the code:

<form method="post" action="abc.php">
<input type="submit" name="sb1" value="sb1"> //<----LINE1
<input type="image" src="sample.jpg" name="sb2" value="sb2"> //<---LINE2
</form>

Now the question is:
I can use submit button to submit "sb1", but if I try to submit image "sample.jpg", I cannot retrieve data "sb2". Anyone knows why? thanks.

Dani AI

Generated

Short answer: an input type="image" does not submit its value like a normal submit button. When clicked the browser sends the click coordinates instead (fields named like name.x and name.y), so you will not receive a sb2 key on the server. This is defined by the HTML spec and explained on MDN (WHATWG spec, MDN reference).

As showed, the regular submit button sends a named value; as and hinted, the issue is how the server-side code looks for the input. To confirm, dump the incoming POST data and inspect the keys. Example PHP troubleshooting pattern:

// inspect request to see exactly what keys arrived
var_dump($_POST);

// detect which control was used
if (isset($_POST['sb1'])) {
    // sb1 submit used
} elseif (isset($_POST['sb2.x']) || isset($_POST['sb2_x']) || isset($_POST['sb2.y']) || isset($_POST['sb2_y'])) {
    // image submit used (check both dot and underscore variants)
}

If a string identifier is required instead of coordinates, add a hidden input or set a hidden field via onclick, or use a normal submit button visually styled with CSS. Note: keyboard-activated submits may not provide coordinates, so rely on an explicit hidden value when you need a predictable server-side flag.

Recommended Answers

All 2 Replies

you probably want to try the php forum

It's definately a problem with the server-side app.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.