Skip to content Skip to sidebar Skip to footer

2x Submit Buttons To Action Different Url

Need help with jquery to change my forms action depending on with SUBMIT Button press. I found some javascript code but its not working.
submit handler, you're trying to call a function called onsubmitform:

<formname="myform"onsubmit="return onsubmitform();"><!--                                 ^--- here     -->

...but your function is actually called submitform:

<scripttype="text/javascript">functionsubmitform()
//       ^--- here

If you make those names match, it'll probably mostly work, although I'm not entirely sure expecting the submit button's click event to fire prior to the form's submit handler is entirely reliable cross-browser.

FWIW, though, I'd probably do it differently. Since you say you're using jQuery, I'd ditch the global submitform function entirely, add a hidden field, and make the buttons just buttons (rather than submit buttons) since your form is already non-functional unless JavaScript is enabled.

So:

<inputtype="hidden" name="operation" value="" />
<inputtype="button" value="Save" />
<inputtype="button" value="Print" />

with:

jQuery(function($) {
    var form = document.myform;
    $(form).find("input[type='button']").click(function() {
        form.operation = this.value;
        form.action = this.value == 'Save' ? 'jbupdate.php' : 'ppage.php';
        form.submit();
        returnfalse;
    });
});

Solution 2:

The following javascript function takes the URL of the target page and an associative array of name/values pairs and POSTs the data to the supplied URL by dynamically creating a form and then submitting it.

You can use instead of submit, two input buttons. On click of the buttons, u can mannualy change the action part to whatever you want, and then submit it.

var myForm = document.getElementById(formid);
 myForm.action = to-wherever-u-want ;
 myForm.submit() ;

Write two functions, with different actions, or write one function, determine which btn was pressd.

Your script was having some errors like that you called onsubmitform(), but you only have sumitform() in defenition. Pls take care of that too.

Solution 3:

Here's another option:

<form action="ThisActionIsReplacedInTheButtonOnclickEvent" method="POST" Name="MyForm" target="_blank" >
...
<inputtype="submit" name="MyBtn" value="do it" OnClick="document.forms['MyForm'].action = 'Destination1.aspx'" >
<inputtype="submit" name="MySecondBtn" value="do it different" OnClick="document.forms['MyForm'].action = 'Destination2.aspx'" >

BTW, IE doesn't seem to like inline js much, so you may have to break the form.action assignment out into a separate function like this:

<inputtype="submit"name="doitbtn"value="do it"OnClick="SetDest1()" /><SCRIPTLANGUAGE="JavaScript">functionSetDest1() {
            document.forms["MyForm"].action = "Destination1.aspx";
        }

        functionSetDest2() {
            document.forms["MyForm"].action = "Destination2.aspx";
        }

    </script>

Solution 4:

More simply:

<inputtype="submit" formaction="someaddress/whataver" value="Do X"/>
<inputtype="submit" formaction="/another/address" value="Do Y"/>

Solution 5:

Probably a tweaked version of the below code will help. I added the alerts for testing.

$('form[name="myform"] input[type="submit"]').click(function(event){
       var $form = $('form[name="myform"]');
       if($(this).val()=='Print'){
           alert('print was pressed')   ;
           $form.attr('action','jbupdate.php');
       }
        else{
            alert('save was pressed')   ;
            $form.attr('action','ppage.php');
        }
});

//I added the following code just to check if the click handlers are called //before the form submission, The alert correctly shows the proper value.
$('form[name="myform"]').submit(function(event){
    alert("form action : " + $(this).attr('action'));
    //event.preventDefault();
});

You can play with the fiddle at http://jsfiddle.net/ryan_s/v4FtD/

Post a Comment for "2x Submit Buttons To Action Different Url"