SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Retrieving checkboxs

    I have a database with tables of states and counties. The first page produces a dynamically created list of check boxes. The visitor checks which counties they select and I want to display those choices (and insert them into another table). I am getting a blank page. I am not getting any error messages.

    This is the code that produces the checkboxes

    PHP Code:
    if ( isset($_POST['submit']) ) { // if form is submitted, process it


    print "<form action=\"phpForm3.php\" method=\"POST\">\n";



    if (
    $Recordset1) {
    print 
    "<table width=200 border=0>\n";

    print 
    "<th>&nbsp; </th>\n";


    print 
    "</tr>\n";
    //create table
    $i 0;
    while ( 
    $row mysql_fetch_array($Recordset1) ) {
    $i++;
    print 
    "<tr>\n";

    print 
    "<td><input type=\"checkbox\" name=\"county$i\" value=\"$row[name]\"></td>\n";

    /*echo "<td>{$row['state_id']}</td>\n";*/

    echo "<td>{$row['name']}</td>\n";

    echo 
    "</tr>\n";

    }
    //end while
    print "</table>\n";
    } else {
    echo(
    "<P>Error performing query: " .
    mysql_error() . "</P>");

    }
    print 
    "<input type=\"hidden\" name=\"counties\" value=\"$i\"/>\n";
    print 
    "<input type=\"submit\" name=\"submit\" value=\"Go\"/>\n";
    }
    ?> 
    This is the query and the code that is to display the data:

    PHP Code:
    $query_county_result "SELECT * FROM counties WHERE name = 'checked'";
    $county_result mysql_query($query_county_result$assess_remote) or die(mysql_error());
    $row_county_result mysql_fetch_assoc($county_result);
    $totalRows_county_result mysql_num_rows($county_result);

    if ( !empty(
    $_POST['county']))
      foreach ( 
    $_POST['county'] AS $id => $name )
        echo 
    'You have selected '{$name}".'<br />'
    -end

    Anyone see where this is going wrong?

    Thank you for your help.

    Gary
    Last edited by ScallioXTX; Feb 24, 2011 at 11:45. Reason: wrapped code in [php]...[/php]

  2. #2
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are checking for $_POST['county'], but your checkboxes are named "county$i", i.e. "county0", "county1", etc.
    aaron-fisher.com - PHP articles and more

  3. #3
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to name the checkboxes county[] - This will mean the browser submits the checkboxes as an array which you can then loop through as demonstrated in your php code.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your reply, I solved the issue by changing

    <input type=\"checkbox\" name=\"county\" value=\"$row[name]\">";

    to

    <input type=\"checkbox\" name=\"county[$i]\" value=\"$row[name]\">";

    Thank you very much for your help.

  5. #5
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No! You don't even need an integer inside the [] symbols.

    All you need is [] - as you see it. Two square brackets with NOTHING inside. Thats all you need and then you can dynamically output them and process them. You don't need to provide any form of indexing for them at all.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    SEPA
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So it is working with the integer in the brackets, is there any advantage / disadvantage to taking it out?

    Thank you for your follow up

    Gary

  7. #7
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Saving bandwidth for one

    It serves no purpose really because if you're generating the checkboxes dynamically then unless you're keeping a list/array of the checkboxes that were output in the session then there is not much point outputting them with an index unless you're checking that index against something on the server.

    If you're not keeping any records of what checkboxes you've issued anywhere then all you're doing is actually bogging down phps code execution and parsing time because its having to parse your checkbox output string and replace $i with a number which in reality you don't need anyway once the form is submitted.

    When the form is submitted you can just use foreach() to loop through the array automagically as you have been doing anyway and thus the need for any form of index in is not needed.

    You can still do it if you want to, you're not actually doing anything wrong BUT why do it unless you're processing it?
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.


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
  •