Submit a form on 2 different action pages

I would like to have a form that can submit to two different action pages based on a selection window.

Value, if I select in the selection box Products, the action will be products.html, and if I choose Users, the action will beusers.html

I found many examples that have two submit buttons, but I only need one submit button.

Any ideas on how to do this?

+3
source share
4 answers

you can use jQuery for this ...

if the selected value is equal to something

set the form attribute action for another thing, not the initial action

this, of course, is pseudo code.

here is a working solution:

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#selectList").change(function(){
        if($('#selectList').val() == 1){
        $("#yourform").attr("action","secondaryaction");
        }
    });
});
</script>
</head>
<body>
<select name="dummy" id="selectList">
<option value="0">foo</option>
<option value="1">bar</option>
</select>

<form id="yourform" action="primaryaction">
bla bla
</form>
</body>
</html>
+5
source

- ( , myForm):

document.myForm.onsubmit = function() {
    if (this.mySelector.value == 'products') {
        this.action = 'products.html';
    }
    // the "else" isn't necessary: leave it at "users.html" as a default.
};
+1

read about the new attributes Attributes for submitting the form, which can be specified in the submit buttons. Attributes are: formaction, formenctype, formmethod, formnovalidate and formtarget

Works in IE> = 11

my example for will be demonstrated:

<form action="1.php">
  <input type="text" >
  <button value="go"> GOOOOOOOOOOOO</button>
 <button value="go" formaction="2.php"> DONNNNNNNNNNNNNNNNOOOO</button>
</form>
0
source
if(condition==products)
document.forms[0].action = 'products.html;
else
document.forms[0].action = 'users.html;
-1
source

Source: https://habr.com/ru/post/1736966/


All Articles