An HTML form can submit to different action pages without changing the form with JavaScript. Put the normal destination in the form's action attribute, then use formaction on another submit button to override that destination.
A submit button can also use formtarget="_blank" when its response should open in a new browsing context, usually a new tab. These submit-button attributes override the form's corresponding settings only for the button that was used.
<form method="post" action="test5.php">
<label for="name">Name</label>
<input type="text" id="name" name="name" value="plus2net">
<button type="submit">Submit to test5.php</button>
<button type="submit" formaction="test6.php">Submit to test6.php</button>
</form>
The first button uses the form's normal action="test5.php". The second button overrides it with formaction="test6.php". No JavaScript is required.
Add formtarget="_blank" to the submit button that should display its response in a new browsing context. Browsers commonly use a new tab, but the exact presentation is controlled by the browser and user settings.
<button type="submit"
formaction="test6.php"
formtarget="_blank">
Open test6.php in a new tab or window
</button>
For a page focused specifically on this behavior, see submitting a form to a new window or tab.
This updated demo keeps the original Plus2net endpoints. The first button submits to test5.php in the current page. The second submits the same form data to test6.php in a new tab or window.
The form-level attributes provide defaults. A submit button can override selected submission settings for that one submission.
| Form setting | Submit-button override | Purpose |
|---|---|---|
action | formaction | Destination URL |
method | formmethod | HTTP method such as GET or POST |
enctype | formenctype | Encoding used for submitted form data |
target | formtarget | Where the response is displayed |
novalidate | formnovalidate | Whether browser constraint validation is skipped |
For this tutorial, formaction and formtarget are the important overrides. Avoid adding the others unless the different action genuinely requires them.
If both buttons submit to the same server endpoint but request different operations, you usually do not need different action URLs. Give the buttons the same name and different values so the server can identify which submit button was used.
<form method="post" action="process.php">
<button type="submit" name="task" value="preview">Preview</button>
<button type="submit" name="task" value="save">Save</button>
</form>
The clicked submit button contributes its name/value pair to the submission. The server must still validate that requested operation before performing it.
The older version of this page changed form.action and form.target from inline onclick handlers. Modern HTML already has declarative attributes for this exact job, so JavaScript is unnecessary.
Using formaction and formtarget also avoids accidentally leaving a modified action or target on the form after one button has been clicked.
name to contribute a value to the submitted form data.Opening a separate tab is not always necessary. If the user can preview safely within the same workflow, that may provide a simpler experience.
For normal multiple-destination forms, use formaction instead of changing the form's action from an onclick handler.
A JavaScript-assigned target can remain on the form for later submissions. A button-specific formtarget applies only when that submit button is used.
_blank requests a new browsing context. The browser may display it as a new tab or window.
POST changes how data is transmitted in the HTTP request; it does not encrypt the connection. Use HTTPS and validate data on the server.
Yes. Put one destination in the form's action attribute and use formaction on another submit button to override the destination for that submission.
No. Submit buttons support the formaction attribute, which overrides the form's action when that button is used.
Add formtarget="_blank" to that submit button. The browser decides whether the new browsing context appears as a tab or a separate window.
Yes. formaction changes the destination URL. Unless a button also overrides the method with formmethod, the form's method continues to apply.
Give submit buttons a name and different values. The activated submit button's name and value are normally included in the submitted form data.
No. POST does not encrypt the connection. Use HTTPS and validate submitted data on the server.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| kundan | 27-06-2014 |
| nice help me alot thanks | |
| osiris | 17-08-2014 |
| very nice helped me alot thx | |
| bol | 12-09-2014 |
| You are the best | |
| vishnu | 20-09-2014 |
| this is very useful thanks i got my problem solution from here .. | |
| ekke | 16-10-2014 |
| Yupp, it helped to solve my problem. Thanks! | |
| peter | 09-12-2014 |
| thanks man! :) | |
| san | 17-04-2015 |
| this is very useful thanks i got my problem solution from here .. Thank you so much..... :) Keep us updating such nice codes | |
| pratish | 12-11-2018 |
| +1...it helps me a lot if......Can it redirect with _POST method will all the form field name? If another php want to POST all form field. | |
| smo1234 | 25-11-2018 |
| You can add all form field names to the form. After receiving you can redirect to any page. | |
25-04-2020 | |
| this is genius, thanks a lot. | |