Forms with multiple submits

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mevets
Forum Newbie
Posts: 23
Joined: Fri Sep 15, 2006 10:06 am

Forms with multiple submits

Post 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?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 : 
   }
}
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
mevets
Forum Newbie
Posts: 23
Joined: Fri Sep 15, 2006 10:06 am

Post 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.';
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply