Results 1 to 5 of 5

Thread: Which submit button was pressed? (OR, Which row to operate on?) [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Question 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.
    Last edited by Phenix; Feb 25th, 2003 at 02:28 PM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    What you can do, in php, while generating those submit buttons is to do something like this:

    PHP Code:
    echo "<input type=button onClick=\"javascript:document.location.href=\'submitted.php?id=$variablename;\" name=$variablename>"
    So this way, each button calls the page as, say

    submitted.php?id=3292384

    You can work with the values thereonwards....

    Is that what you're looking for?

  3. #3

    Thread Starter
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228
    Thanks for your reply, mendhak. I'm close to a PHP solution, but I'm trying to clean it up by keeping the value as "EDIT" instead of "EDIT46". I'm also finding out how much of a newb I am at PHP.

    How do get name1 and name2 instead of value1 and value2 using the POST method (instead of the GET method)? I thought there is a way to get that. In your example, the question would be how to get the text "id" as a variable name to use as $id in PHP. This code gives me the value instead of the name.

    Code:
    foreach ($_POST as $val){
    printf("$val");
    }
    ...
    printf("<TR>
    	<TD><P ALIGN=\"CENTER\">%s
    	</TD>
    	<TD>%s</TD>
    	<TD>%s</TD>
    	<TD><P ALIGN=\"CENTER\">%s
    	</TD>
    	<TD>%s</TD>
    	<TD NOWRAP><INPUT TYPE=\"SUBMIT\" NAME=\"edit%s\" VALUE=\"EDIT%s\"><INPUT TYPE=\"SUBMIT\" NAME=\"delete%s\" VALUE=\"DELETE%s\"
    	</TR>", $myrow[0], $myrow[3], $myrow[2], $myrow[1], $myrow[4], $myrow[0], $myrow[0], $myrow[0], $myrow[0]
    );

  4. #4

    Thread Starter
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Thumbs up 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"

  5. #5
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: NOTE TO SELF: (and maybe some RESOLUTIONS) I'll have to try this tomorrow:

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width