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
because all of those are form events.
Should I attach my $.post() to the form.submit() event, and if so, how do I tell which input the user clicked on, or
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!? });
yes, multiple submit buttons can include in the html form. One simple example is given below.
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.
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.
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)
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) }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With