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

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


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



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

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


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

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

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

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

echo "</tr>\
";

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

}
print "<input type=\\"hidden\\" name=\\"counties\\" value=\\"$i\\"/>\
";
print "<input type=\\"submit\\" name=\\"submit\\" value=\\"Go\\"/>\
";
}?>

This is the query and the code that is to display the data:

$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

you are checking for $_POST[‘county’], but your checkboxes are named “county$i”, i.e. “county0”, “county1”, etc.

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.

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.

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.

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

Saving bandwidth for one :wink:

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? :wink: