开发者

Different form ACTION depending on button pressed

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 i开发者_如何学Gof I press button A or button B.

How can I do this?

Thanks!


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜