Home » Php » php – How to submit a form to two different pages depending on the button clicked, without javascript

php – How to submit a form to two different pages depending on the button clicked, without javascript

Posted by: admin July 12, 2020 Leave a comment


I have a form with a text area and 2 buttons, i need one of them to submit to the same page its on and the other to submit to another php file. Both buttons need to allow the text areas to be referenced by post. How can i do this.

For Example:

<form action="" method="post">


   <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


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

How to&Answers:

if (isset($_POST['action1']) || isset($_POST['action2'])) {
    // handle textarea

    if (isset($_POST['action1'])) {
        header('Location: /action1.php');

    header('Location: /action2.php');


    <textarea name="text"></textarea>
    <input type="submit" name="action1" value="Action1">
    <input type="submit" name="action2" value="Action2">

You only have to be cautious about what happens when the user presses the enter key to submit the form. I.e. what submit will be triggered.


Give each submit button a name (e.g button1 and button2).
Submit your form to a single php script.
On this php script, check the $_POST vars to see which button was clicked.
Then act accordingly, processing your form values and redirecting to whatever page you want afterwards.

To check which button was clicked, do something like:

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


If your using CodeIgniter there are better ways to do this. (another post for CodeIgniter points to this as being duplicate so thought I’d add a better solution for CI.)

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


Here is the form buttons on the cart form screen:

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


The form action points to a controller with a method save ("cart/save/"). Save simply determines which button was clicked and forwards to the appropriate cart function to handle form submission.


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

Within the controller (cart) I simply then have update(), clear() and checkout() functions which the save() method calls. There is no need to redirect, via CI’s redirect() or php header redirects etc.

I hope that helps someone


you can do it from the server side (as answered before me), you can do it from client side as my answer:(you can even do both):

<form action="" id="frm" method="post">
   <input type='button' onclick="submitMe(this)" value='Preview'>
   <input type='button' onclick="submitMe(this)" value='Save'> 

function submitMe(obj){
  if(obj.value == "Preview"){
   document.getElementById('frm').action = 'preview.html'
   document.getElementById('frm').action = 'save.html'


Do it on the server side via a redirect.