I have a form and I would like the ACTION field to be different depending on the button pressed.
For instance the form might get processed by different PHP files if I press button A or button B.
How can I do this?
Thanks!
Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.
yes, multiple submit buttons can include in the html form. One simple example is given below.
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.
If you don't want to use Javascript, but do use HTML5, you can use the attribute formaction
:
<!DOCTYPE html>
<html>
<body>
<form>
<input type="submit" formaction="http://firsttarget.com" value="Submit to first" />
<input type="submit" formaction="http://secondtarget.com" value="Submit to second" />
</form>
</body>
</html>
In your buttons you can just set the form's action, using it's form
property, for example on button a:
this.form.action = "fileA.php";
On the other:
this.form.action = "fileB.php";
You can rig this up externally, like this:
document.getElementById("buttonA").onclick = function() {
document.getElementById("myForm").action = "fileA.php";
};
Or if you're using a library like jQuery:
$(function() {
$("#buttonA").click(function() {
$(this).closest("form").attr('action', 'fileA.php');
});
});
Leave the action field blank:
<form action ="" method="post" name="form1">
<input type ="submit" onclick="calA();"/>
<input type = "submit" onclick="calB"/>
</form>
<script>
function calA()
{
document.form1.action ="a.php";
}
function calB()
{
document.form1.action = "b.php";
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With