Page 1 of 1

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

Posted: Fri Mar 28, 2008 12:51 pm
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 />";
}
 

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

Posted: Fri Mar 28, 2008 1:23 pm
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.

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

Posted: Fri Mar 28, 2008 2:06 pm
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

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

Posted: Fri Mar 28, 2008 2:26 pm
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>

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

Posted: Fri Mar 28, 2008 2:48 pm
by tejasadmin
thank you very much. It worked :-)