Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

submitting form on image click

Tags:

html

I have multiple images inside one form. Depending on the image clicked a specific value should be sent with a POST.

Can this be done without javascript? (I use jquery, if not).

I tried something like this:

<input type="image" name="type" value="1" src="images/type1.jpg" />

But only the coords of the image were sent, not type=1.

like image 423
domino Avatar asked May 04 '12 10:05

domino


People also ask

Can you use an image as a submit button?

Definition and UsageThe <input type="image"> defines an image as a submit button. The path to the image is specified in the src attribute.

How do you submit a form on click?

You don't need to use JavaScript to do submit button or input on entering key pressed. Just need to mark it up with type="submit" , and the other buttons mark them with type="button" .

How do I automatically submit a form without clicking?

Submit a Form Using JavaScript The most simple way to submit a form without the submit button is to trigger the submit event of a form using JavaScript. In the below example we are going to create a function to submit a form. We will set that function at onclick event of a div tag.

What happens when you click submit on a form?

The form will be submitted to the server and the browser will redirect away to the current address of the browser and append as query string parameters the values of the input fields.


2 Answers

The safe approach to this problem is to give the inputs unique names and see which one sends coordinates.

<input type="image" name="submit_blue" value="blue" alt="blue" src="blue.png">
<input type="image" name="submit_red"  value="red"  alt="red " src="red.png">

Only the successful control will send any data. So test to see if submit_blue.x has a value, then test if submit_red.x has one.

There is no need to involve JavaScript at all.

Alternatively, use regular submit buttons instead of server side image maps.

<button name="action" value="blue"><img src="blue.png" alt="blue"></button>

… keeping in mind that this will break in old-IE as it doesn't support <button> properly.

like image 196
Quentin Avatar answered Oct 24 '22 18:10

Quentin


Using jQuery you can submit the clicking on the image. You can add a id for the image or even a classe.

$( "#yourImageId" ).click(function() {
  $( "#yourFormId" ).submit();
});
like image 5
Michel Moraes Avatar answered Oct 24 '22 19:10

Michel Moraes