Jump to content
  • Advertisement
Sign in to follow this  
InsaneBoarder234

[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.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!