Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change form action on select option

I want to change the form action based on a selection value.

<form name="store" id="store" method="post" action="">
<select name="storeID">
<option value="/stores/store6.php">6</option>
<option value="/stores/store10.php">10</option>
</select>                   
</form>

Now I want the form action, to use the select option value. For example:

If Selection 1 is selected, use the folowing form action /stores/store6.php

like image 889
daniel. Avatar asked Sep 21 '14 15:09

daniel.


4 Answers

Add this to your select:

onchange="changeAction(this)"

and this to your javascript

changeAction = function(select){
   document.getElementById("store").action = select.value;
}
like image 175
Johan Karlsson Avatar answered Sep 20 '22 17:09

Johan Karlsson


You can use the onchange event to change the form's action

document.getElementById('store').storeID.onchange = function() {
    var newaction = this.value;
    document.getElementById('store').action = newaction;
};

Here is a jsfiddle with the code.

like image 32
Tasos K. Avatar answered Oct 16 '22 22:10

Tasos K.


Add to select onchange function

<select name="storeID" onchange='changeAction(this.value)'>

and add to javascript

function changeAction(val){
    document.getElementById('storeID').setAttribute('action', val);
}

This will change action after selected option is changed to selected option value.

like image 2
Jozef Dúc Avatar answered Oct 16 '22 20:10

Jozef Dúc


<form name="store" id="store" method="post" action="" id="FORM_ID" >
    <select name="storeID">
        <option value="/stores/store6.php">6</option>
        <option value="/stores/store10.php">10</option>
    </select>                   
</form>

<script type="text/javascript">
document.getElementById('storeID').onchange = function(){
    document.getElementById('FORM_ID').action = '/'+this.value;
}
</script>

check it out i wish it will work ..

like image 2
Ferrakkem Bhuiyan Avatar answered Oct 16 '22 22:10

Ferrakkem Bhuiyan