Which submit button was pressed? (OR, Which row to operate on?) [RESOLVED]
I am using PHP to create an HTML form using a table. Each row has a column with buttons labelled EDIT and DELETE. I can name the buttons and their values according to the row they are in. I would like to know which button is pressed to submit the form.
Webmonkey has a way to see if a variable exists, but I haven't found a way to make that work for me in this case.
http://hotwired.lycos.com/webmonkey/...tw=programming
I think the problem is that I need to set some variable to identify which button was pressed to submit the form at the time the button is pressed. I was thinking that JavaScript could help, but maybe it should all be able to be done in PHP.
I am interested in any solution in any language. This must be a typical problem when using databases. How do I allow the user to choose which row(s) to edit or delete (after displaying the rows)?
Or maybe each row would be its own FORM, but that seems like the wrong way.
NOTE TO SELF: (and maybe some RESOLUTIONS) I'll have to try this tomorrow:
The webmonkey technique is deprecated.
if($submit)
is now
if($_POST['submit'])
or
if($_GET['submit'])
depending upon the FORM ACTION GET or POST.
The rest of this post I'll have to try tomorrow:
Alternate foreach technique for key/value pairs.
Regarding getting the variable name instead of the value:
What I have been calling the variable name should be a "key" accessible with this form of the foreach statement:
foreach(array_expression as $key => $value) statement
In my case:
foreach($_POST as $key => $val)
echo "$key : $val";
And the apparently more general list:
foreach ($GLOBALS as $var){
foreach ($var as $element => $value){
echo "$element : $value ";
}
}
While searching for a solution to "clean up", I found:
1) To use one variable name (for example "edit") - Add brackets to the variable name for each identically named row:
name="edit[]" value="edit1"
name="edit[]" value="edit2"
Use foreach technique to display variables to seee how it should be accessed (or search php.net again).
2) Using the "key", search for substring (strstr) of "edit" in the key variable name (using the technique I used on the value variable name). This should work for my case instead of having EDIT40 and EDIT50 etc. on the button text.
name="edit1" value="EDIT"
name="edit2" value="EDIT"
Re: NOTE TO SELF: (and maybe some RESOLUTIONS) I'll have to try this tomorrow:
Quote:
Originally posted by Phenix
While searching for a solution to "clean up", I found:
1) To use one variable name (for example "edit") - Add brackets to the variable name for each identically named row:
name="edit[]" value="edit1"
name="edit[]" value="edit2"
Use foreach technique to display variables to seee how it should be accessed (or search php.net again).
that is how you want to do it. add it as an array.