0

I have an array of checkbox in a table in PHP file like this :

echo "<td $Blocked><input type =\"checkbox\" name=\"Blocked[]\" value=\"checkblock\" /></td>";

I am trying to get the value of number of checked checkboxes and save it to DB.

$Blocked = 'unchecked';

if ((isset($_POST['edit_tc']))) {
    if (isset($_POST['Blocked'])) {
        if (is_array($_POST['Blocked'])) {
            foreach($_POST['Blocked'] as $value) {
                error_log($value);
            }
        }
        else {
            $value = $_POST['Blocked'];
            error_log($value);
        }

        $Blocked = 'checked';
    }
}

"edit_tc" is the Submit button.

How do I take the number of it when the user checks the checkbox & clicks Submit button to save it to a table column?

BVee
  • 44
  • 9

3 Answers3

0

You can use this method...

if ((isset($_POST['edit_tc']))) {
  if (isset($_POST['Blocked'])) 
  {
    $data = $_POST['Blocked'];
   foreach ($data as $checkedValue)
   {
     $qry = mysql_query("INSERT INTO `Table_name` WHERE `column_id` = '$checkedValue'");
   }
  }
}

This is the method that will use to save each value as different records... You can modify that if needed.

DeDevelopers
  • 581
  • 1
  • 7
  • 25
0

I guess below code will solve all your problem.

<?php
$hello = array();
if(isset($_POST['submit'])) {
  extract($_POST);
  print_r($hello); //print all checked elements
}
?>

<form method="post">
    <input type="checkbox" name="hello[]" value="1" <?php if(in_array(1, $hello)){ echo 'checked'; } ?>>
    <input type="checkbox" name="hello[]" value="2" <?php if(in_array(2, $hello)){ echo 'checked'; } ?>>
    <input type="checkbox" name="hello[]" value="3" <?php if(in_array(3, $hello)){ echo 'checked'; } ?>>
    <input type="checkbox" name="hello[]" value="4" <?php if(in_array(4, $hello)){ echo 'checked'; } ?>>
    <button type="submit" name="submit" value="Submit">Submit</button>
</form>

You have to check the value of each checkbox inside the checkbox array you are getting using in_arrayfunction of PHP.

If you need to understand whole thing, just let me know.

Will be glad to help you out.

Regards.

0

You can simply use count() function to get total number of checkbox checked by user. As only those checkbox values are posted to submitted page which are checked by user and if checkbox is not checked , its value will not be submitted. i.e The unchecked checkbox will not exist on submitted page.

USE:

    $totalCheckboxChecked=count($_POST['Blocked']);
Sharry India
  • 341
  • 1
  • 9