SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    php and html for distinguishing between multiple submit buttons of same name?

    i'm trying to do a table, each row containing some data followed by its own 'edit' and 'delete' buttons. so there's an edit button and delete button on each row. this is what the outputted html, which is contained in a post form, currently looks like:
    Code:
    <tr>
    	<td>Some</td>
    	<td>data</td>
    	<td>here</td>
    	<td><input type="submit" name="edit-1" value="Edit" /></td>
    	<td><input type="submit" name="delete-1" value="Delete" /></td>
    </tr>
    <tr>
    	<td>Some</td>
    	<td>data</td>
    	<td>here</td>
    	<td><input type="submit" name="edit-2" value="Edit" /></td>
    	<td><input type="submit" name="delete-2" value="Delete" /></td>
    </tr>
    i thought i'd be able to then extract the name value but that doesn't seem to be passed on to the receiving script. is that correct? i get the value value; Edit or Delete. i suppose i could put the number in the button but i'd prefer not to. anyway of passing on which of the multiple buttons was clicked without putting the value visibly on the button? i suppose i could have multiple forms, one for each row, which would also do it but i'm wondering if there's a simpler way?

    thanks.

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    <input type='submit' name='Submit_1' value='Edit' />
    <input type='submit' name='Submit_2' value='Delete' />

    PHP Code:

    $_POST
    ['Submit_1'];
    $_POST['Submit_2']; 

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    i see, i think. so if i had many submit buttons in the same way as the html you've posted, the code to find out which submit button was clicked might be something like:

    Code:
    $numberofpossiblebuttons = ...
    
    $i = 0;
    while( $i < $numberofpossiblebuttons && empty($submitbutton) ) {
    	if( isset($_POST['Submit_' . $i]) ) $submitbutton = $i;
    	$i++;
    }
    is that the/a way to do it? i'll try it.

    thanks.

  4. #4
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    excellent yes that basically worked. had to contruct the name string before putting it the $_POST array index: $name = 'Submit_' . $i..

    thanks.

  5. #5
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    It might be easier to read the code to do this unless you have bunch of submit buttons

    PHP Code:

    if (isset($_POST['delete'])) {
        
    // delete whatever
    } else if (isset($_POST['edit'])) {
        
    // edit whatever


    HTML Code:
    <input type='submit' name='delete' value='Delete' />
    <input type='submit' name='edit' value='Edit' />

  6. #6
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also name your submit buttons in an array format. This will enable you to get your values without creating extra loops in your PHP.
    Code:
    <tr>
        <td>Some</td>
        <td>data</td>
        <td>here</td>
        <td><input type="submit" name="submit[1]" value="Edit" /></td>
        <td><input type="submit" name="submit[1]" value="Delete" /></td>
    </tr>
    <tr>
        <td>Some</td>
        <td>data</td>
        <td>here</td>
        <td><input type="submit" name="submit[2]" value="Edit" /></td>
        <td><input type="submit" name="submit[2]" value="Delete" /></td>
    </tr>
    Now you can determine which button was clicked fairly easily since PHP will want to treat it as an array with only one element.
    PHP Code:
    if (isset($_REQUEST['submit'])) {
        
    /**
         * $_REQUEST['submit'] is an array with only one element.
         * We can get the key for this "array" to find the number of the
         * submit element.
         */
        
    $submitNumber array_pop(array_keys($_REQUEST['submit']));

        
    // Now the value, "Edit" or "Delete"
        
    $submitValue array_pop($_REQUEST['submit']);

    An example php file:
    PHP Code:
    <html>
        <body>
            <form action="<?php echo $_SERVER['PHP_SELF'?>" method="post">
                <p>
                    Row 1
                    <input type="submit" name="submit[1]" value="Edit" />
                    <input type="submit" name="submit[1]" value="Delete" />
                </p>
                <p>
                    Row 2
                    <input type="submit" name="submit[2]" value="Edit" />
                    <input type="submit" name="submit[2]" value="Delete" />
                </p>
            </form>

            <?php if (isset($_REQUEST['submit'])): ?>

            <dl>
                <dt>Submitted row number:</dt>
                <dd><?php echo array_pop(array_keys($_REQUEST['submit'])) ?></dd>

                <dt>Submitted value:</dt>
                <dd><?php echo array_pop($_REQUEST['submit']) ?></dd>
            </dl>

            <?php endif; ?>

        </body>
    </html>
    BTW - This style of element naming does not work if you use quotes for the array keys in your HTML:
    Code:
    <!-- This does not work properly -->
    <input type="submit" name="submit['2']" value="Edit" />
    Last edited by Trent Reimer; Jun 27, 2007 at 11:19.

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wonshikee View Post
    It might be easier to read the code to do this unless you have bunch of submit buttons
    not only do i have a bunch of submit buttons but also i have a variable number so it can't be hard coded like that. thanks.

  8. #8
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Trent Reimer View Post
    Just to continue on what johnyboy is saying: you can also name your submit buttons in an array format. This will enable you to get your values without creating extra loops in your PHP.
    yes i thought there must be something like that which allows you to refer to it directly once somehow. i was quite surprised i couldn't ask for it by the html attribute's name, "name" in this case but it doesn't look like that doesn't get transmitted with the form submission.

    great, thanks

  9. #9
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    works a treat. i was surprised that the square brackets in the name attribute validated though. i just wrote a function which turns a string into an appropriate string for use in name attributes and when doing so referred to http://www.w3.org/TR/html4/types.html#type-name which says
    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
    no square bracket characters mentioned. but it does validate so that's that.

    thanks.

  10. #10
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    no square bracket characters mentioned. but it does validate so that's that.
    I'm glad you looked that up, I never even thought of it. Good to know.

  11. #11
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    i made a mistake: the ID and NAME bit i quoted from the html w3 specs is not at all related to the name attribute. the character data type that is applicable to name attributes in the html specs is CDATA, and "CDATA is a sequence of characters from the document character set and may include character entities." so that obviously includes square brackets.


Bookmarks

Posting Permissions

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