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?
Forms with multiple submits
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I would go with Begby's suggestion..
Something like
Something like
Code: Select all
if (isset($_POST['submit']))
{
switch ($_POST['submit']))
{
case 'insert' : break;
case 'delete'; break;
default :
}
}Awesome! All I have to do is
Code: Select all
if isset($_POST['btnName'])
echo 'The button with the name attribute btnName was clicked.';- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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...
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.
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
...
}
?>