views:

161

answers:

3

Hi,

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

Meaning if I select in the select box Products, the action would be products.html, and if I select Users, the action would be users.html

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

Any ideas on how to do it?

+4  A: 

you can use jQuery for that ...

if selected value equals something

set form attribute action to the other thing not initial action

this is pseudo code of course ..

here is a working solution :

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"&gt;&lt;/script&gt;
<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</value>
<option value="1">bar</value>
</select>

<form id="yourform" action="primaryaction">
bla bla
</form>
</body>
</html>
c0mrade
Thank you, just add the submit button :D
Adnan
A: 
if(condition==products)
document.forms[0].action = 'products.html;
else
document.forms[0].action = 'users.html;
balalakshmi
A: 

Try something like this (assuming a form named "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.
};
nickf