Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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
}
else if(array_key_exists('add', $_REQUEST))
{
// Add New
}
?>


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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this