0

I have a todo list app. It has a column called completed which has true or false values. I want the checkbox to be checked if completed is true. I came up with the following:

echo "<label>".$row["name"]."</label>
      <input onload='choose(".$row["completed"].")' class='sssss' value='".$row['id']."' type='checkbox'/>";

And

<script src='js/jquery.js'></script>
<script>
function choose(a) {
  $(this).attr('checked', a);
}
</script>

But it isn't working. Please help

Edit

I believe that the js code is only using the first column value for all checkboxes

3 Answers 3

2

You can use set the check with PHP:

echo "<label>".$row["name"]."</label>
  <input class='sssss' value='".$row['id']."' type='checkbox' ". ($row['completed'] == 'true') ? 'checked' : ''." />";

The ternary $row['completed'] == 'true' ? 'checked' : '' will echo checked if it's true, and nothing if it's not.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. But my column has either true or false as the value (false is the default value that can't be checked)
That's what this does. empty checks to see if the variable exists and if it is equal to 0, empty text ('') or false. So !empty is 1) Does it exist (yes) 2) does it contain a positive value (yes). So it will echo 'checked' if the value is true. And I'm editing the answer to use the correct index (oops).
No I mean the column is VARCHAR
0

Use this:

echo "<label>".$row["name"]."</label>
<input ".($row["completed"]) ? 'checked' : ''." class='sssss' value='".$row['id']."' type='checkbox'/>";

Comments

0

I figured out a different way:

if ($row['completed'] == 'true'){
    echo "<input class='sssss' value='".$row['id']."' type='checkbox' disabled checked/>";
}else {
    echo "<input class='sssss' value='".$row['id']."' type='checkbox' disabled />";
}

Comments

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.