I have a checkbox field in my HTML table. The table is generated dynamically, and the field is stored in an array as follows:
<input type="checkbox" name="checked[]" value开发者_Go百科="1">
In the PHP, I am inserting the checked rows into the table, but I also need to display the rows that have not been checked to the user after the submission, but there is no way of knowing which rows were not passed since checked[]
for unchecked checkboxes are not being submitted.
What I want to achieve is the user is displayed a table with multiple rows, he checks which rows he wants to add to the database. After form submission, a page is to display which rows were inserted and which rows were not selected by the user. The unchecked rows need not be inserted into any database, but should be displayed to the user only ONCE, right after the submission, so that he can print the page for record purpose.
What is the best way to tackle this problem?
since you are creating the chekbox in first place so you know the total number of checkbox.
Total Checkbox minus Checked checkbox will give you what you want i.e unchecked box.
unchecked boxes = total checkboxes - checked checkboxes ;
Use 3 arrays, 1 holding the options you are sending, one with the responses from the user and one empty that will hold the difference. The difference between the two will get you the unchecked fields.
<?php
$myOptions = array('option1', 'option2', 'option3');
$userArray = array('option2'); // This is your $_POST['checkboxes'] array
$leftOptions = array();
foreach ($myOptions as $value){
if (!in_array($value, $userArray)){
$leftOptions[] = $value;
}
}
?>
Note: you can replace the foreach with array_diff as @deceze mentioned in the comment.
精彩评论