0

I try to add checked in my checkbox, if a value in checkbox is same with the value from database:

My database : Column = fees_month[] Value = '$row[month]'

My Default checkbox :
echo "<th >"."<input type='checkbox' name='fees_month[]' value= '$row[month]' checked='isChecked('$row[month]',$fees_month)' >". $row['month']."</th>";

$q = mysqli_query($conn,"SELECT * FROM fees WHERE registration_number='$qry[registration_number]'");

while ($info = mysqli_fetch_array($q)) {
    $fees_month = explode(", ", $info['fees_month']);
}

function isChecked($val, $arr) {
    if (in_array($val, $arr)) {
        echo 'checked';
    }
}
2
  • @danila, Do you have any suggestion where I`m wrong? Commented Aug 15, 2015 at 21:54
  • $q = mysqli_query($conn,"SELECT fees_month FROM fees WHERE registration_number='$qry[registration_number]'"); while($info = mysqli_fetch_array($q)) { $fees_month = explode(",", $info['fees_month']); function isChecked($val, $arr) { if(in_array($val,$arr)) { echo 'checked'; } } } echo "<th >"."<input type='checkbox' name='fees_month[]' value= '$row[month]' checked='isChecked($val,$arr);'>". $row['month']."</th>"; Commented Aug 16, 2015 at 3:11

3 Answers 3

1
$selected = array();
$pdo = new PDO('mysql:host=localhost;dbname=your_db', 'user', 'password');

$query = $pdo->prepare('
    SELECT fees_month 
      FROM fees 
     WHERE registration_number = :registration_number
');

$query->bindParam('registration_number', $qry[registration_number]);

while ($row = $query->fetch()) {
    $selected[] = $row['fees_month'];
}

$checked = in_array($active, $selected) ? 'checked="checked"' : '';
echo '<input type="checkbox" name="fees_month[]" ' . $checked . ' value="' . $active . '"/>';

// example for testing
$active = 1;
$selected = array(1, 10, 20);
$checked = in_array($active, $selected) ? 'checked="checked"' : '';

echo '<input type="checkbox" name="fees_month[]" ' . $checked . ' value="' . $active . '"/>';
Sign up to request clarification or add additional context in comments.

1 Comment

Its not working, printing only 'checked' several times without space.
0

isChecked is not a variable so variable substitution doesn't work here.

You'll have to do something like the following:

'" . isChecked(<params>) . "'

instead.

1 Comment

Use the code @danila-ganchar provided. It's my solution without the need for you to do something for yourself.
0

Html checkbox sends value "on" on submitting if checked. If you are directly saving the submitted value, than you should check if the value is on.

For example variable $checkbox stores the value that you retrieved from database, you can use something like this :

<?php $isChecked = $checkbox == 'on' ? 'checked="checked"' : '';
echo "<input type='checkbox' $isChecked>";
?>

1 Comment

Can you supply more information ? How are you saving the info (if it is checked) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.