how to have 1 form with 2 submit buttons
hi all, how do you make 1 form with 2 submit buttons? I can show the buttons, of course, but how to handle the code?
I have a form that contains a number of items with checkboxes, and when the user clicks Delete button, the checked items will be deleted. It's no problem because I can just use the code <form action=delete.php ....>.
Now, the problem is, I want to make another button besides the Delete button. I want to have a Move button, so that when the user clicks the Move button, the checked items in the form will be moved to another category, not delete them.
I'm confused how to do that. You can't have a form with two actions (delete.php and move.php), right?
thanks for your help
Re: how to have 1 form with 2 submit buttons
The best way to do this is to submit to one script and create an action array form the submit buttons:
Code:
<input type="submit" name="action[delete]" vlaue="Delete" />
<input type="submit" name="action[move]" value="Move" />
You can then use the following code to check for what action is required:
PHP Code:
if (is_array($_POST['action'])) {
$action = array_keys($_POST['action']);
$action = $action[0];
}
swtich ($action) {
case 'move':
do_move();
break;
case 'delete':
do_delete();
break;
}
Some would say, just check the value the action variable, however, if you ever wanted to rename your submit button you will have to rewrite your code.
Re: how to have 1 form with 2 submit buttons
Actually, I tend to use <button> elements in these situations.
Code:
<button type="submit" name="action" value="move">Caption here</button>
<button type="submit" name="action" value="copy">Other caption here</button>
No ugly hacks, no rewriting.
Re: how to have 1 form with 2 submit buttons
Woopz, didn't read the topic good enough. (Deleted my reply ;) )
Re: how to have 1 form with 2 submit buttons
Here's anoher way to do it: http://thomasdamgaard.dk/pub/html/multiple-submit/
It uses a bit of java script to submit to different pages dependig on which button was pressed