Javascript to change form post action URL.

center

Hello there everybody!
This tutorial is Javascript that allows to modify the action field of an HTML form dynamically.
It is possible to have one submit button in a form but different action url.

The example below show how to switch the action post based on the radio button click.

Add the below sample form to your page:
This is an example search form.

<form method="post" onsubmit="formaction(this)"> <br />
<input type="radio" id="action1" name="search" onclick="keyword1.disabled=false; keyword2.disabled=true; this.form.action = 'action1.php';"><br />Action One <br />
<input disabled="disabled" type="text" name="keyword1" size="40"><br />
<input type="radio" id="action2" name="search" onclick="keyword2.disabled=false; keyword1.disabled=true; this.form.action ='action2.php';"><br />Action Two <br />
<input disabled="disabled" type="text" name="keyword2" size="40"> <br />
<input type="submit" border="0" name="search" id="search" value="Search"> <br />
</form>
– DEMO –

Action One

when you select Action One radio button the form will submit to action1.php,

Then for the other radio button when you select this will be submitted to action2.php for this script “ onclick=”this.form.action = ‘action2.php’;”

Action Two

when you select Action Two radio button the form will submit to action2.php,

This will automatically swith the form action url by onlick radio button.

Thank you and Enjoy!

 

Share



Post a Comment

You must be logged in to post a comment.