# [web] Forms - Pass different action depending on button?

This topic is 4513 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I haven't touched HTML in a while and even more so HTML forms and I'm working on a little project. Basically I have a list within a form and the user is to choose an item from that list and then click one of three buttons. How would I go about sending a value as an argument to a PHP file depending on the button that was clicked? So for example, item "abc" is selected in the list and button "Delete" is clicked, the URL "xyz.php?item=abc&action=delete" is opened - or similar. Thanks!

##### Share on other sites
Depends on how you set up your buttons.

<input type="submit" name="delete" value="Delete" />
<input type="submit" name="update" value="Update" />
<input type="submit" name="new" value="Add New" />

And then

<?php
if(array_key_exists('delete', $_REQUEST)) { // Do Delete } else if(array_key_exists('update',$_REQUEST))
{
// Do Update
}
{
}
?>

It's not quite as nice as just comparing action to 'delete', 'update', or 'new' but it's easier to setup.

If you want to use the action method you could use javascript to set a hidden action field to the value you want using an onclick.

<input type="hidden" name="action" value="default" />
<input type="submit" name="delete" value="Delete" onclick="SetAction('delete'); return true;" />

Please forgive my js, It has been quite a while since I've used it but you should be able to get the point.

function SetAction(action)
{
document.formname.action.value = action;
}

It's also worth noting the JS version will not work if the browser has JS turned off.

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632998
• Total Posts
3009811
• ### Who's Online (See full list)

There are no registered users currently online

×