简体   繁体   中英

Submit same form with three buttons using php

I am having a problem to submit same form three time but with different actions. Please see the following code:

    if(isset($_POST['btn_submit'])){
         //Some code here
    }
    if(isset($_POST['btn_sub'])){
        // Some other code here
    }

my html code is as follows:

    <form name="form" id="form" action="" method="" enctype="multipart/form-data">
      <table>
      <tr>
          <!-- Some input fields -->
          <input type="submit" name="btn_submit" id="btn_submit" value="Submit" />
      </tr>
      <tr>
          <!-- Some more input fields -->
          <input type="submit" name="btn_sub" id="btn_sub" value="Submit" />
      </tr>
      </table>
    </form>

Now when I clicked on first submit button is clicked then I got the posted values in POST array (with button name ie. $_POST['btn_submit']=> Submit ) but when clicked on second button then also got all posted values in POST array except name of the button (ie. $_POST['btn_sub'] => Submit ). On second button click I want to update some database records but because of its condition if(isset($_POST['btn_sub'])) not true so it's not enter into this condition and my database records are not updated, rest of the array elements shows but not the submit button.

This functionality is work on my local server but after uploading it on live server its not working.

I don't understand why this is happened, please help me for this.

Try this code

<form name="form" id="form" action="" method="POST" enctype="multipart/form-data">
      <table>
      <tr>
          <!-- Some input fields -->
          <input type="submit" name="btn_submit" id="btn_submit" value="Submit2" />
      </tr>
      <tr>
          <!-- Some more input fields -->
          <input type="submit" name="btn_sub" id="btn_sub" value="Submit1" />
      </tr>
      </table>
    </form>

Use same name for submit button

<form name="form" id="form" action="" method="POST" enctype="multipart/form-data">
  <table>
    <tr>
      <!-- Common input fields -->
      <input type="text" name="commontxt" id="commontxt" value="common text field" />
    </tr>
    <tr>
      <!-- Some input fields -->
      <input type="submit" name="btn_submit" id="btn_submit1" value="Submit 1" />
    </tr>
    <tr>
      <!-- Some more input fields -->
      <input type="submit" name="btn_submit" id="btn_submit2" value="Submit 2" />
    </tr>
  </table>
</form>

Check which submit button is click by this way

if ( !empty( $_POST ) )
{
  if ($_POST['btn_submit'] == 'Submit 1') {
    //action for Submit 1 here
    $commontxt = $_POST['commontxt'];
    echo 'Submit 1<br/>';
    echo $commontxt;
  } else if ($_POST['btn_submit'] == 'Submit 2') {
    //action for Submit 2 here
    $commontxt = $_POST['commontxt'];
    echo 'Submit 2<br/>';
    echo $commontxt; 
  }   
}

There are many approaches you can follow here::

Approach 1:

If you can change the page on submit, means on click of submit request goes to two different pages. There is new HTML5 approach to handle this:

<button type="submit" formaction="/action_one">SUBMIT</button>
<button type="submit" formaction="/action_two">SUBMIT</button>

Apparently this does not work in IE9 and earlier, but for other browsers you should be fine (see: w3schools.com HTML formaction Attribute).

Approach 2

Javascript Method:

<input type="submit" value="dosomething" onclick="javascript: 
form.action='actionurl1';"/>
<input type="submit" value="dosomethingelse" onclick="javascript: 
form.action='actionurl2';"/>

Approach 3

If you want to keep same page on submit:

<input type="submit" name="row[1]" value="Submit">
<input type="submit" name="row[2]" value="Submit">

And then in the server side (PHP in my example) you can read "row" as an array to get the index:

$index = key($_POST['row']);

$_POST['row'] will be an array with just one element, in the form index => value (for example: '2' => 'something").

Read Here About Index Here

=========================== New Answer as per OPs approach ==================

I have tried your Form Structure and i am getting second button name perfectly.

<?php 
if(isset($_POST['btn_submit']) && empty($_POST['btn_sub']) ){
     echo "First Submit-----------<br/>";
     print_r($_POST);
}
else if(isset($_POST['btn_sub']) && empty($_POST['btn_submit'])){
    echo "Second Submit-----------<br/>";
    print_r($_POST);
}

?>

HTML

<form name="form" id="form" action="" method="post" enctype="multipart/form-data">
  <table>
  <tr>
      <input type="text" value="Form1" name="form1_input" />
      <input type="submit" name="btn_submit" id="btn_submit" value="Submit" />
  </tr>
  <tr>
      <input type="text" value="Form2" name="form2_input" />
      <input type="submit" name="btn_sub" id="btn_sub" value="Submit" />
  </tr>
  </table>
</form>

Check this out and also confirm if you are nor repeating the same name on the page...

My output on second submit::

Second Submit-----------
Array ( [form1_input] => Form1 [form2_input] => Form2 [btn_sub] => Submit ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM