Dynamic Form Actions using Different Buttons

DEPRECATED: This post has been marked as deprecated and may no longer contain industry best-practices.

For my NeoInvoice project, we had a need to change the form action based on which button is being pressed. Sure, we could have had the resulting URL determine which action to take based on the clicked button name in the POST array, but we have a specific URL nomenclature to maintain.

This simple solution uses JavaScript to check which button was pressed, thereby changing the form action right before the form gets submitted. This solution has been tested in both Firefox and IE8.

<form method="post">
    <input type="text" name="items" /><br />
    <input type="submit" value="Delete" onClick="this.form.action='/action/delete';" /> |
    <input type="submit" value="Touch" onClick="this.form.action='/action/touch';" /><br />
</form>
Thomas Hunter II Avatar

Thomas is the author of Advanced Microservices and is a prolific public speaker with a passion for reducing complex problems into simple language and diagrams. His career includes working at Fortune 50's in the Midwest, co-founding a successful startup, and everything in between.