views:

42

answers:

1

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!

+1  A: 

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');
  });
});
Nick Craver
+1 I especially liked your usage of `closest` in `jQuery`.
Jacob Relkin
Can I combine the POST and GET methods? For instance, I want to pass a URL containing fields but also the form elements by their names.
posfan12
@posfan12 - You can only submit via one method at a time, though you could serialize and post to a url with params. I'm not following you on why you would ever need to have both though, it's more data for the client to send as well and generally not a great idea.
Nick Craver
Awesome thanks!
posfan12