7

I have many forms like the following on the page. Now I want change the form action based on which submit button the user clicked (and of course submit the form)

<form action="/shop/products.php" data-ajax="false" method="post">
    <div data-role="fieldcontain">

        <input name="submit" class="obutn" type="submit" value="Order" />
        <input name="submit" class="oEbutn" type="submit" value="Extras" />

    </div>
</form>

I tried with

$(".obtn").click(function() {
    $(this).parent().parent().attr("action", "/shop/products.php");     
});
$(".oEbtn").click(function() {
    $(this).parent().parent().attr("action", "/shop/extras.php");       
});

but the form is always submited to products.php. Can you tell me what's wrong?

000
  • 26,951
  • 10
  • 71
  • 101
Bad Pussycat
  • 386
  • 1
  • 4
  • 14
  • 8
    You've misspelt the class names. – dsgriffin Jul 15 '13 at 18:50
  • You could use .closest('form') instead of the two parent() selectors to clean it up a bit – Justin Bicknell Jul 15 '13 at 18:52
  • Instead of setting the ```action``` attribute on the form itself, consider setting ```formaction``` attribute on each individual ```input``` element. Docs: http://www.w3.org/TR/html-markup/input.submit.html#input.submit.attrs.formaction – xorcus Nov 19 '15 at 10:16

5 Answers5

13

Instead of setting the action attribute on the form itself, consider setting formaction attribute on each individual input element.

Docs: http://www.w3.org/TR/html-markup/input.submit.html#input.submit.attrs.formaction

<form data-ajax="false" method="post">
    <div data-role="fieldcontain">
        <input formaction="/shop/products.php" 
               name="submit" class="obutn" type="submit" value="Order" />
        <input formaction="/shop/extras.php" 
               name="submit" class="oEbutn" type="submit" value="Extras" />
    </div>
</form>
xorcus
  • 999
  • 1
  • 11
  • 12
7

There are two typos:

  • obtn instead of obutn
  • oEbtn instead of oEbutn

Another suggestion is to use closest("form") to get the form that contains the clicked button.

$(".obutn").click(function() {
    $(this).closest("form").attr("action", "/shop/products.php");     
});
$(".oEbutn").click(function() {
    $(this).closest("form").attr("action", "/shop/extras.php");       
});

$("form").on("submit", function () {
    alert($(this).attr("action"));
});

JSFIDDLE

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
2

Capture the submit event and determine which button was clicked. From that, you can change the action of the form.

Here's a link on how to do that.

How can I get the button that caused the submit from the form submit event?

Community
  • 1
  • 1
JeffRegan
  • 1,322
  • 9
  • 25
1

Also, don't give the form the action until the click happens at all, it is superfluous.

<form data-ajax="false" method="post">
   <div data-role="fieldcontain">

        <input name="submit" class="obutn" type="submit" value="Order" />
        <input name="submit" class="oEbutn" type="submit" value="Extras" />

    </div>
</form>
Michael Draper
  • 1,928
  • 3
  • 18
  • 24
0

What if you try it out with a switch instead? Something like:

<input name="submit" id = "1" class="obutn" type="submit" value="Order" />
<input name="submit" id = "2" class="oEbutn" type="submit" value="Extras" />

And then in JavaScript we have:

//param: The Id attr of the button that was clicked
function postTo(id){
  switch(id){
   case 1: postProducts();
           break;
   case 2: postExtras();
           break;

   default: //Do something else 

  }
}

Just an idea. Haven´t tested that yet, but maybe it could be helpful. I hope so.

user1659653
  • 334
  • 2
  • 5
  • 15