How to Change a Form Action in Javascript
The "action" property of a form determines what page processes your Web form. The action property of a form can be dynamically set using Javascript. Javascript is a lightweight programming language that runs on the user's browser. Action variables are changed dynamically after the user inputs criteria into the browser. The form change only takes a few lines of code in a Javascript block.
Instructions
-
-
1
Create a script block that holds the Javascript function. All Javascript functions are contained within specialized tags. The following code is placed within the "<head>" and "</head>" tags:
<script type="text/javascript"> </script> -
2
Create the function that changes the form's action. The following code changes the form's action:
function changeAction(form)
{
form.action = "ProcessPage.php"
}
Change the "ProcessPage.php" with the name of your web page that processes the form variables. -
-
3
Insert the new function code into the "Submit" button for your form. Every form has a button that submits the elements for processing. When you insert Javascript into the submission button, the Javascript is called before the page is processed, which allows you to change the action. Below is a sample of a submission button:
<input type="submit" onClick="return changeAction(this);">
-
1
References
- Photo Credit browser (child with notebook sit green grass) image by STOLBTSOV ALEXANDRE from Fotolia.com