tags:

views:

122

answers:

3

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

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

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.

Adnan
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 submitif(!isset($_POST['edit_prefs'])) { $mewmsg = $_POST['chk'][newmsg1]; mysql_query("UPDATE social_members_prefs SET m_newmsg='".$newmsg."' WHERE p_id='".$m_id."'");}
Patrick
A: 
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" <?php if ($_POST['chk']['newmsg2']): ?>checked="checked"<?php endif; ?> />
Michal M
+2  A: 

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 it's value has been submitted.

e.g.

if ($_POST['chk']['newmsg2'] == 1) { 
David Dorward