Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit form to another page (which is different from the page used in ACTION)

Tags:

I have a form like this:

index.php

<form method="post" action="send.php">   <textarea name="msg" id="msg"></textarea>   <input type="submit" value="Send" /> </form> 

So, if I enter something in textarea and clicked on "Send", it is submitted to "send.php" page. But I want to include another button for previewing it. That is, when this button is clicked, the above form is submitted to "preview.php" which will be opened in a new blank window/tab (original page ie. index.php will be there intact). This is to display a preview of the message, that the user is going to send.

I do not know how to do this.

like image 557
Vpp Man Avatar asked Jan 04 '12 18:01

Vpp Man


People also ask

How do I submit a form to another page?

To link a submit button to another webpage using HTML Form Tags: Using the HTML Form's Action Attribute, we can link the submit button to a separate page in the HTML Form element. Here, declare/write the “Submit button” within HTML Form Tags and give the File Path inside the HTML form's action property.

Which method is used to post of form to another page?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.

Can I have two or more actions in the same form in HTML?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.


2 Answers

Use Javascript to temporarily change the action and target:

<form method="post" action="send.php" id="idOfForm">   <textarea name="msg" id="msg"></textarea>   <input type="submit" value="Send" /> </form> <button onclick="doPreview();">Preview</button> <script type="text/javascript">     function doPreview()     {         form=document.getElementById('idOfForm');         form.target='_blank';         form.action='preview.php';         form.submit();         form.action='send.php';         form.target='';     } </script> 
like image 59
Aaron J Spetner Avatar answered Oct 24 '22 14:10

Aaron J Spetner


There is now an attribute for the submit input that handles this:

<input type="submit" formaction=”differentThanNormalAction.php”> 
like image 43
Noumenon Avatar answered Oct 24 '22 14:10

Noumenon