when php is called check what button is clicked on a dynamic

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tejasadmin
Forum Newbie
Posts: 4
Joined: Fri Mar 28, 2008 12:44 pm

when php is called check what button is clicked on a dynamic

Post by tejasadmin »

I have created a php that will query a database and give values and a submit button aka

[submit] John Doe

i create the submit button using

Code: Select all

 
echo "<input name='$col_value' type='submit' value='Select' />  ";
 
this works fine but when i go back in to click the button i dont know how to tell php what button i pushed. it would be easy to do an if statement but these could be buttons from 1 - 1000 and i dont want to write that many if statements

Can anyone help please

the code looks like this

Code: Select all

 
$query = mysql_query(
"SELECT `uniqid`,
`F_Name`,
`L_Name`
FROM `people`
ORDER BY `L_Name` ASC"
) or die (mysql_error());
$count = 0;
while ($line = mysql_fetch_array($query, MYSQL_ASSOC)) {
   //echo "\t<tr>\n";
   
    foreach ($line as $col_value) {
    
    if ($count == 0){
    echo "<input name='$col_value' type='submit' value='Select' />  ";
    $count ++;
    }else{
    $nameval = ($col_value . " ");
    echo "$nameval";
    
       //echo "$col_value ";
    }
    
    }
    $count = 0;
    echo "<p>";
    //echo "\t</tr>\n<br />";
}
 
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Re: when php is called check what button is clicked on a dynamic

Post by hawleyjr »

Change the value of the button to something relative to the field. ex: a database index corresponding to that row in the table.
tejasadmin
Forum Newbie
Posts: 4
Joined: Fri Mar 28, 2008 12:44 pm

Re: when php is called check what button is clicked on a dynamic

Post by tejasadmin »

The database value corresponds to a uniq id number. The field name is ID. and the values in that field will range from 1- 1000.

is there not a way to do a check on the name of the button clicked
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: when php is called check what button is clicked on a dynamic

Post by pickle »

Rather than naming your button $col_value, name it button[$col_value] (in double-quotes so $col_value will be evaluated). When the form is POSTed, $_POST will contain an element called 'button'. That element will itself be an array. The key of that sub-array (there will only be one as only 1 button was clicked) will be the $col_value of the button that was clicked.

Code: Select all

<html>
 <head>
 </head>
 <body>
  <form method = 'post' action = ''>
   <?PHP
   $ids = array(1,2,300,1000);
   foreach($ids as $curr_id)
   {
    echo "<input type = 'submit' name = 'buttons[$curr_id]' value = 'Select' />";
   }
   ?>
  </form>
 </body>
</html>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
tejasadmin
Forum Newbie
Posts: 4
Joined: Fri Mar 28, 2008 12:44 pm

Re: when php is called check what button is clicked on a dynamic

Post by tejasadmin »

thank you very much. It worked :-)
Post Reply