To dynamically switch 'action' in an HTML Form, it possible to have more than one submit button in a form.
The following Javascript code shows how to handle multiple submit buttons:
The following Javascript code shows how to handle multiple submit buttons:
<script type="text/javascript">
function OnSubmitForm()
{
if(document.pressed == 'Insert')
{
document.myform.action ="insert.html";
}
else if(document.pressed == 'Update')
{
document.myform.action ="update.html";
}
return true;
}
</script>
<form name="myform"onsubmit="return onsubmitform();">
<input type="submit" name="operation" onclick="document.pressed=this.value" value="insert"/>
<input type="submit" name="operation" onclick="document.pressed=this.value" value="update"/>
</form>
function OnSubmitForm()
{
if(document.pressed == 'Insert')
{
document.myform.action ="insert.html";
}
else if(document.pressed == 'Update')
{
document.myform.action ="update.html";
}
return true;
}
</script>
<form name="myform"onsubmit="return onsubmitform();">
<input type="submit" name="operation" onclick="document.pressed=this.value" value="insert"/>
<input type="submit" name="operation" onclick="document.pressed=this.value" value="update"/>
</form>
Alternatively we can also decide it based on the radio buttons, see the example below:
<script type="text/javascript">
function OnSubmitForm()
{
if(document.myform.operation[0].checked == true)
{
document.myform.action ="insert.html";
}
else if(document.myform.operation[1].checked == true)
{
document.myform.action ="update.html";
} return true; } </script>
<form name="myform" onsubmit="return OnSubmitForm();">
name: <input type="text"name="name">
email: <input type="text"name="email">
<input type="radio" name="operation" value="1" checked> insert
<input type="radio" name="operation" value="2"> update
<p>
<input type="submit" name="submit" value="save">
</p>
</form>
function OnSubmitForm()
{
if(document.myform.operation[0].checked == true)
{
document.myform.action ="insert.html";
}
else if(document.myform.operation[1].checked == true)
{
document.myform.action ="update.html";
} return true; } </script>
<form name="myform" onsubmit="return OnSubmitForm();">
name: <input type="text"name="name">
email: <input type="text"name="email">
<input type="radio" name="operation" value="1" checked> insert
<input type="radio" name="operation" value="2"> update
<p>
<input type="submit" name="submit" value="save">
</p>
</form>