4

How do I check a checkbox?

I've tried 1, On, and Yes. That doesn't work. Putting the worked "checked" alone works, but then how do I check with PHP after a form post of the checkbox is checked?

<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />

3 Answers 3

4

A checkbox will only be a successful control if it is checked.

Controls that are not successful are not submitted as data.

Therefore, you can tell if a checkbox is checked by seeing if its value has been submitted.

E.g.

if ($_POST['chk']['newmsg2'] == 1) {
Sign up to request clarification or add additional context in comments.

Comments

0
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" <?php if ($_POST['chk']['newmsg2']): ?>checked="checked"<?php endif; ?> />

Comments

0

Here is the code;

<form action="test.php" method="POST">
    <input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
    <input type="submit">
</form>

<?php
    $check = $_POST['chk']['newmsg2'];
    echo "***$check****"
?>

If it is checked $check will show 1.

1 Comment

nothing is being updated? like you said in your example it should return 1 when checked. I checked the box and submited the form and nothing was saved to the database. the mysql update is correct, I tested it with a forced manual insert and its correct. this is my form: <input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] /> and then i did a simiple test php mysql submit if(!isset($_POST['edit_prefs'])) { $mewmsg = $_POST['chk'][newmsg1]; mysql_query("UPDATE social_members_prefs SET m_newmsg='".$newmsg."' WHERE p_id='".$m_id."'"); }

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.