Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submitting a jQuery ajax form with two submit buttons

Tags:

I have a form that looks like this:

<form action="/vote/" method="post" class="vote_form">     <input type="hidden" name="question_id" value="10" />     <input type="image" src="vote_down.png" class="vote_down" name="submit" value="down" />     <input type="image" src="vote_up.png" class="vote_up" name="submit" value="up" /> </form> 

When I bind to the form's submit ($("vote_form").submit()), I don't seem to have access to which image the user clicked on. So I'm trying to bind to clicking on the image itself ($(".vote_down, .vote_up").click()), which always submits the form, regardless of whether I try

  • return false;
  • event.stopPropogation(); or
  • event.preventDefault();

because all of those are form events.

  1. Should I attach my $.post() to the form.submit() event, and if so, how do I tell which input the user clicked on, or

  2. Should I attach my $.post() to the image click, and if so, how do I prevent the form from submitting also.

Here is what my jQuery code looks like now:

$(".vote_up, .vote_down").click(function (event) {     $form = $(this).parent("form");     $.post($form.attr("action"), $form.find("input").serialize() + {         'submit': $(this).attr("value")     }, function (data) {         // do something with data     });     return false; // <--- This doesn't prevent form from submitting; what does!? }); 
like image 487
worksology Avatar asked Sep 01 '09 22:09

worksology


People also ask

Can I have two or more Submit buttons in the same form?

yes, multiple submit buttons can include in the html form. One simple example is given below.

How do I create a two button form?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

How can use multiple submit button in PHP?

Having multiple submit buttons and handling them through PHP is just a matter of checking the the name of the button with the corresponding value of the button using conditional statements. In this article I'll use both elseif ladder and switch case statement in PHP to handle multiple submit buttons in a form.

Can you have multiple submit buttons in a form MVC?

There are multiple ways to use multiple submit buttons on a single view in mvc. Here you will learn about the most 3 popular ways to use multiple submit buttons on a single view. # First way (using a different name on all submit buttons)


1 Answers

Based on Emmett's answer, my ideal fix for this was just to kill the form's submit with Javascript itself, like this:

$(".vote_form").submit(function() { return false; }); 

And that totally worked.

For completeness, some of my JS code in the original post need a little love. For example, the way I was adding to the serialize function didn't seem to work. This did:

    $form.serialize() + "&submit="+ $(this).attr("value") 

Here's my entire jQuery code:

$(".vote_form").submit(function() { return false; }); $(".vote_up, .vote_down").click(function(event) {     $form = $(this).parent("form");     $.post($form.attr("action"), $form.serialize() + "&submit="+ $(this).attr("value"), function(data) {         // do something with response (data)     }); }); 
like image 165
worksology Avatar answered Oct 18 '22 18:10

worksology