SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checkbox's got me down

    OK, here's the problem. I have a page (search1.php) which receives input from the user, then searches my database table according to their criteria, and then passes the values on as variable to another page (search2.php) for further processing. This second page displays the results of their search as a table, with the last column containing a checkbox and a submit button. The idea being that if their search returns multiple results, the user can select the checkbox(s) and click the submit button which will save their selection(s) back in the database, which can then be drawn upon when the user is finished searching and wishes to see all of their selections displayed.
    Here is where I encounter the problem. If in fact their search returns multiple results, each row in the table (each item) will contain it's own checkbox and submit button. If the user wants to select multiple items they have to click each checkbox and submit button individually in order for the database to be updated with their selection. Simply put, I want one submit button which will submit all of their checked items. The other parts of the script seem to work properly except for this.
    Here is the part of the code from the search2.php page that produces the checkbox/submit button.
    A bit wordy - but there it is. Any wisdom to impart? Thanks in advance!

    <?php
    while ( $row = mysql_fetch_array($result) )
    {$id = $row["id"];
    $title = $row["title"];
    $composer = $row["name"];
    $artist = $row["artist"];
    $instrument = $row["instrument"];
    echo("<form action=\"search1.php\" method=\"post\">
    <TR bgcolor=\"ffffcc\">
    <TD><div align=\"center\"><a href=\"sqorder.html\">$title</a></div></TD>
    <TD><P><div align=\"center\">$artist</div></TD>
    <TD><div align=\"center\">$composer</div></TD>
    <TD><div align=\"center\">$instrument</div></TD>
    <TD><INPUT TYPE=\"Checkbox\" align=\"middle\" name=\"check\" value=\"$id\">
    <INPUT TYPE=\"Submit\" align=\"middle\" name=\"Submit\"value=\"Add\"></TD>
    </TR>
    </form>"); }

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One thing you could do is make the name for the checkboxes be check[]. Then when you submit it, $check will be an array with all of the ids that you need. Just use a for loop to submit em all to your database or do whatever you want.

  3. #3
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply Anarchos. I wasn't sure how you intended 'check[]' to be used. I've changed the name of the check box to 'check' and changed the original code to:

    <?php
    while ( $row = mysql_fetch_array($result) )
    {$id = $row["id"];
    $title = $row["title"];
    $composer = $row["name"];
    $artist = $row["artist"];
    $instrument = $row["instrument"];

    echo("<form action=\"search1a.php\" method=\"post\">
    <TR bgcolor=\"ffffcc\">
    <TD><div align=\"center\"><a href=\"sqorder.html\">$title</a></div></TD>
    <TD><P><div align=\"center\">$artist</div></TD>
    <TD><div align=\"center\">$composer</div></TD>
    <TD><div align=\"center\">$instrument</div></TD>
    <TD><INPUT TYPE=\"Checkbox\" align=\"middle\" name=\"check\" value=\"$id\"></TR>"); }

    echo("<tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td><INPUT TYPE=\"Submit\" align=\"right\" name=\"Submit\"value=\"Submit\"></TD></tr></form>");
    ?>
    This fixed the problem of having multiple submit buttons by removing the Submit line of HTML from the loop and adding it at the end.
    But, if multiple items are checked on the result page only the last one checked will be updated in the database. Is the code I've inserted into the processing page the problem? Here it is:

    if (@$Submit)

    $checkboxs = array ("$check");
    foreach($checkboxs as $item)
    {$sql="update cds set cart='YES' where id=$item";}

    Many thanks!

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2001
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change this line

    <TD><INPUT TYPE=\"Checkbox\" align=\"middle\" name=\"check\" value=\"$id\"></TR>"); }

    to

    <TD><INPUT TYPE=\"Checkbox\" align=\"middle\" name=\"check[]\" value=\"$id\"></TR>"); }

    and I am not sure, but I don't think you will need this line

    $checkboxs = array ("$check");

    because $check will be an array already.

    I think you can just do:

    while ( !empty($check) ) {
    $sql="update cds set cart='YES' where id='$check'";
    }
    Last edited by Marty H.; Jul 19, 2001 at 12:12.
    Marty H.

  5. #5
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Closer to checkbox nirvana

    The problem seems to be that the original code assumes that a single variable can store multiple values. Changing this line in the form:

    <TD><INPUT TYPE=\"Checkbox\" align=\"middle\" name=\"check\"
    value=\"$id\"></TR>");

    To this:

    <TD><INPUT TYPE=\"Checkbox\" align=\"middle\" name=\"check\[$id]\" value=\"$id\"></TR>"); }

    And this line in the processing script:

    $checkboxs = array ($check);
    foreach($checkboxs as $item)
    {$sql="update cds set selection='YES' where id=$item";}

    To this:

    while(list($key,$value) = each($check))
    {$sql="update cds set cart='YES' where id=$value";}

    Seems to be closer to fixing the problem because all of the checked items are passed on as $id to the processing script. (Thanks to James Hitz for helping with this!) The problem remains however, that the sql statement only updates the last $id passed on in the database.
    I'm yet too green with PHP to get a handle on the solution here - but many of you aren't


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
  •