How to submit a form to two different pages depending on the button pressed, without javascript

I have a form with a text area and two buttons, I need one of them to send to the same page, and the other to another php file. Both buttons should allow links to text areas by mail. How can i do this.

Example:

<form action="" method="post"> <textarea></textarea> <input type='submit' value='Preview'> //I want this to submit to the same page <input type='submit' value='Save'> // I want this to submit to save.php </form> 

Note. All my html is generated by php through different scripts that change depending on previous user actions.

+6
source share
5 answers
 <?php if (isset($_POST['action1']) || isset($_POST['action2'])) { // handle textarea if (isset($_POST['action1'])) { header('Location: /action1.php'); exit(); } header('Location: /action2.php'); exit(); } ?> <form> <fieldset> <textarea name="text"></textarea> <input type="submit" name="action1" value="Action1"> <input type="submit" name="action2" value="Action2"> </fieldset> </form> 

You only need to be careful about what happens when the user presses the enter key to submit the form. That is, that the message will be launched.

+7
source

Give each submit button a name (for example, button1 and button2). Submit the form to a single PHP script. On this PHP script check $ _POST vars to see which button was clicked. Then act accordingly, processing your form values ​​and redirecting to any page that you want subsequently.

To check which button was pressed, follow these steps:

 if ($_POST["button1"]) { // do stuff } elseif ($_POST["button2"]) { // do other stuff } 
+2
source

you can do it from the server side (as mentioned earlier), you can do it from the client side as my answer: (you can even do both):

 <form action="" id="frm" method="post"> <textarea></textarea> <input type='button' onclick="submitMe(this)" value='Preview'> <input type='button' onclick="submitMe(this)" value='Save'> </form> <script> function submitMe(obj){ if(obj.value == "Preview"){ document.getElementById('frm').action = 'preview.html' }else{ document.getElementById('frm').action = 'save.html' } document.getElementById('frm').submit(); } </script> 
+1
source

If you are using CodeIgniter , there are more efficient ways to do this. (Another post for CodeIgniter indicates that this is a duplicate, so I thought I'd add a better solution for CI .)

In my case, I have a shopping cart form (for example). In this form, I can have buttons: checkout, update cart and clear.

the form:

Here are the form buttons on the cart form screen:

 <input type="submit" value="Update"> <input type="submit" value="Clear"> <input type="submit" value="Checkout"> 

Controller:

The form action points to the controller with the method saved ( "cart/save/" ). Save simply determines which button was clicked, and forward to the appropriate basket function to handle submitting the form.

Cart / Save ()

 public function save() { $submit_button = $this->input->post('submit_button'); if ($submit_button == 'Update') $this->update(); else if ($submit_button == 'Checkout') $this->checkout(); else if ($submit_button == 'Clear') $this->clear(); } 

Inside the controller ( cart ) I just have the functions update() , clear() and checkout() , which the save() method calls. There is no need to redirect through CI () redirection or php header redirection, etc.

I hope this helps someone

+1
source

Do this server side through redirection.

0
source

Source: https://habr.com/ru/post/895010/


All Articles