Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I find out which button is pressed on a form? 1

Status
Not open for further replies.

towerbase

Programmer
Jul 31, 2002
1,053
GB
Using PHP, I produce an HTML page which contains several forms. There's one form for every record selected from a MySQL table. Typically 4 or 5 forms in a web page.

Each form contains two buttons and a text area. One button is for 'Update' and the other button is for 'Delete'. The name (and id) of each button is 'U$key' and 'D$key' respectively where $key contains the primary key of the record displayed in the text area.

When the end user clicks on one of these buttons, how should my PHP script determine which button has been clicked?

I could keep a list of the keys displayed on a page in a session variable and then check the $_REGISTER array. But I was hoping that there is a simpler solution.



Andrew
Hampshire, UK
 
Hi

The clicked button's data is always sent to the server if it has [tt]name[/tt] ( other then empty string ).

If the [tt]form[/tt] is submitted without properly pressing a button, depends on the browser : some will send the first button's data, others nothing.

Feherke.
 
Thanks for the quick reply, feherke.

I am fairly new to PHP and I don't really know how to interpret your answer. Perhaps I wrote the question badly.

Here is a simplified sample of the web page:
Code:
...
<form method='post' action='abc.php'>
<input type='text' name='t123' id='123' value='text123'>
<input type='submit' name='u123' id='u123' value='Update'>
<input type='submit' name='d123' id='d123' value='Delete'>
</form>
<form method='post' action='abc.php'>
<input type='text' name='t456' id='456' value='text456'>
<input type='submit' name='u456' id='u456' value='Update'>
<input type='submit' name='d456' id='d456' value='Delete'>
</form>
...
There are four buttons on the web page. These are created dynamically. Which variable(s) should the script abc.php look at to determine which button was pressed? Is there a (global) variable which tells me the name of the button that was pressed?

Andrew
Hampshire, UK
 
Hi

For example if the first button is pressed :
Code:
<input type='submit' name='u123' id='u123' value='Update'>
then you will have an element in the $_POST array with the 'u123' key and 'Update' value. So you can test for it :
Code:
if (isset($_POST['u123'])) {
  [gray]// do the update[/gray]
}

[gray]// or[/gray]

if ($_POST['u123']=='Update') {
  [gray]// do the update[/gray]
}

Feherke.
 
Thanks again for the reply.

The problem I have is that at the time of writing the script (today) I don't know the keys of the records being displayed at some future time. So I can't hard code in the key such as '123'.

What I want is a function that will return 'u123' (or whatever the name of the button that is being pressed). I could then parse 'u123' to mean update record with key '123'.



Andrew
Hampshire, UK
 
You could try adding a hidden input inside the form which has just the key in it. Then when submitted get the key and test for uKEY or dKEY...you with me?

Code:
<input type='hidden' name='key' value='123'>

Code:
if (isset($_POST["u".$_POST["key"]])) {
    //do update
} else if (isset($_POST["d".$_POST["key"]])) {
    //do delete
}

Obviously you may well want to test for $_POST["key"], but you get the picture ;)
 
Thanks RobHudson. That looks like it might work. I'll give it a try and report back.

Andrew
Hampshire, UK
 
Yes, perfect, Rob. A neat solution. Have a star and some of the grey hairs I've pulled out meanwhile.

Feherke, thanks for your time.


Andrew
Hampshire, UK
 
the alternative is to give each button the same name and then test the value in your php.

Code:
if (isset($_POST['submit'])){
  switch ($_POST['submit']){
    case "something":

  }
//etc

or name them all differently like this

Code:
<input type="submit" name="submit[u123]" value="update">
<input type="submit" name="submit[d123]" value="delete">
and test like so

Code:
[code]
if (isset($_POST['submit'])){
  list ($key, $value) = $_POST['submit'];
  
switch($key){
  case 'd123':
    // do something
  }
//etc

personally, as RobHudson recommends, I always use some form of action parameter. sometimes this is a hidden key and sometimes I name each of my submit buttons as "action" and test for the presence of action in my script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top