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

Started by
0 comments, last by tstrimp 17 years, 10 months ago
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!
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb
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 &#106avascript 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.

This topic is closed to new replies.

Advertisement