Help!
I can't figure this problem out, and my friends have been stumpted as well. As I understand it, if you have an array of checkboxes being dynamically created, and none of them is selected, there is no index in the array, and it therefore cannot be passed.
I have one page that is dynamically created with an array of checkboxes, which the user will be able to check to delete a row from a MySQL database. The other page takes the rows that were selected and deletes them from the MySQL database.
To fix the problem of nothing being initialized, we gave the checkboxes a value of 1 and made a hidden field with the same name as the checkbox array and gave it a value of 0, thus checked = 1 and not checked = 0.
But, the problem we have now is, if you check a checkbox, the value 1 is stored in the next element of the array, but a zero is stored in the element directely after that. The problem then, is if you have a form with THREE data entries, and you check all THREE to delete them, the checkbox array on the second page will have SIX elements, a 1 and 0 for each data entry selected.
If you understood that, this code should help. This what we're doing. It doesn't work right, and it'll take a lot of work to make it work right. Is there an easier way?
for ($i=0; $i <$num_result; $i++) {
$row = mysql_fetch_array($result);
echo "<form name=\"dynamic_form\" method=\"post\" action=\"delete_field.php\">";
echo "<tr><td>";
echo "<input type=\"textbox\" value=\"$row[c_field_name] \" name=\"fn_array[]\">";
echo "</a></td><td>";
echo "<center>";
echo $row["c_field_type"];
echo "</center></td><td>";
echo "<input type=\"textbox\" value=\"$row[c_max_size] \" size=\"6\" name=\"fs_array[]\">";
echo "</td><td>";
echo "<input type=\"textbox\" value=\"$row[c_description] \" name=\"fd_array[]\">";
echo "</td><td>";
//what comes first... the chicken...
echo "<input type=\"checkbox\" value=\"1\" name=\"fc_array[]\">";
//or the checkbox...
echo "<input type=\"hidden\" value=\"0\" name=\"fc_array[]\">";
echo "</td><tr>";
//for loop to set value of IDX array to value of id for table, where the values are the same.
//this for loop compares data in database to that which is being displayed. ie. same thing.
#$sql = "UPDATE container SET c_id = $idx[$i]
#WHERE c_field_name = '$row[c_field_name] '
#AND c_description = '$row[c_description] ' ";
#$res = mysql_query($sql, $connection) or die( "Could not execute field name sql : $sql");
#echo "<input type=\"hidden\" value=\"$row[c_id]\" name=\"id_array[]\">";
}
echo "<td colspan=5>";
echo "<center><input type=\"submit\" value=\"Update\" ><input type=\"reset\" value=\"Reset Values\" > </center>";
echo "</td></tr></tr>";
echo "</form>";
?>
This has been plaguing us for a while now. Any help or suggestions would be greatly(!) appreciated. Thanks for the help.