Page 1 of 1

Forms with multiple submits

Posted: Thu Mar 29, 2007 7:44 am
by mevets
I am writing a small note taking program. The interface is much like many webmail clients. Often in webmail there are checkboxes next to each entry for an email. Then there are multiple buttons such as archive, delete, mark as read, etc. When one of the buttons is pressed the action page, specified in the form tag, must be able to know which of buttons was pressed. Was it Delete, Archive, or Other? This way the script knows what to do with all the checked emails. I am struggling with trying to include multiple submit buttons in 1 form. Is there maybe a way to find out the name attribute of the button pressed. Remeber these buttons are in one form.

Is there a way to do this using one form? Or an I taking the wrong approach?

Posted: Thu Mar 29, 2007 7:55 am
by anjanesh
1. Use Javascript to set an action value of a hidden element when a button gets clicked and then submit() the form

2. Like Gmail, use a drop down (select) of actions and one submit button instead of many submit buttons

Posted: Thu Mar 29, 2007 10:02 am
by Begby
You can have mutliple submit buttons with the same name and different ID's/values. The just check if $_POST['buttonName'] is equal to edit, delete or what have you.

Posted: Thu Mar 29, 2007 10:06 am
by John Cartwright
I would go with Begby's suggestion..

Something like

Code: Select all

if (isset($_POST['submit'])) 
{
   switch ($_POST['submit'])) 
   {
      case 'insert' : break;
      case 'delete'; break;
      default : 
   }
}

Posted: Thu Mar 29, 2007 10:35 am
by anjanesh
Thread :
feyd wrote:It should be noted that checking for the submit button can lead to false negatives due to some browsers not submitting it.

Posted: Thu Mar 29, 2007 10:44 am
by mevets
Awesome! All I have to do is

Code: Select all

if isset($_POST['btnName'])
  echo 'The button with the name attribute btnName was clicked.';

Posted: Thu Mar 29, 2007 4:36 pm
by RobertGonzalez
What I do is have multiple submit buttons, each with a unique name. Before processing anything, I check if there is a form field value set, then I check to see if none of the buttons is set in which case I error. Somthing ala...

Code: Select all

<?php
// Cancellation - Only recourse is to send back to admin messaging page
if (isset($_POST['cancel_message']))
{
	// The admin canceled
	header('Location: ' . $this->append_session_id(PAGE_ADMIN_MESSAGES));
	exit;
}

if (isset($_POST['message_title']) && (!isset($_POST['cancel_message']) && !isset($_POST['create_message'])))
{
	$this->error->clean_die('Please use the form buttons to process your request.');
}

if (isset($_POST['create_message']))
{
	// Start checking inputs
...
}
?>
IE is one of the browsers that does not send button information when a user hits the enter key in a form. Firefox and Opera send the first known submit button if you hit the enter key in the form.