Reputation: 682
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
Upvotes: 0
Views: 547
Reputation: 682
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 />";
}
Upvotes: 0
Reputation: 23011
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.
Upvotes: 2
Reputation: 355
Use this:
echo "<label>".$row["name"]."</label>
<input ".($row["completed"]) ? 'checked' : ''." class='sssss' value='".$row['id']."' type='checkbox'/>";
Upvotes: 0