multiple buttons on a form

Solution from: http://websistent.com/multiple-submit-buttons-in-php/

html:

<input type="submit" name="btn_submit" value="Button 1" />
<input type="submit" name="btn_submit" value="Button 2" />
<input type="submit" name="btn_submit" value="Button 3" />

php:

<?php
if($_REQUEST['btn_submit']=="Button 1")
{
print "You pressed Button 1";
}
else if($_REQUEST['btn_submit']=="Button 2")
{
print "You pressed Button 2";
}
else if($_REQUEST['btn_submit']=="Button 3")
{
print "You pressed Button 3";
}
?>

You check the post or get data from your form, using the name of the button:

<form action='' method='post'>
  <button type='submit' name='reset'>Clear</button>
  <button type='submit' name='submit'>Submit</button>
</form>

PHP (after submission):

if(isset($_POST['reset'])) { /* ...clear and reset stuff... */ }
else if(isset($_POST['submit']) { /* ...submit stuff... */ }

Alternatively, you have two buttons with the same name, which both submit your form, and if/else their values:

<form action='' method='post'>
  <button name='submit' value='0'>Clear</button>
  <button name='submit' value='1'>Submit</button>
  <button name='submit' value='2'>Something Else</button>
</form>

PHP (after submission):

if($_POST['submit']==0)      { /* ...clear and reset stuff... */ }
else if($_POST['submit']==1) { /* ...submit stuff... */ }
else if($_POST['submit']==2) { /* ...do something else... */ }

Tags:

Html

Php