Monday, July 6, 2009

HTML - JavaScript : Button Inputs as Links

Normally Button inputs in HTML post data to a destination link specified when the form is declared. Wouldn't it be nice to have multiple buttons that can post to other URL's on the same form? Most "solutions" in forums and books say create a different form for each button, but that's cumbersome and messes up your page layout. And you are not allowed to "nest" forms in HTML.

Enter JavaScript. The one line solution (for GET method): While declaring your button input include:

onClick="window.location.href='../folder/url'"

for POST method dynamically change the form "action" attribute with:

onClick="document.myform.action='../folder/url'; document.myform.submit();"

This last solution is my favorite because you can have a whole bunch of data in input fields automatically posted to whatever URL you want from just one form.

No comments:

Post a Comment