Uncheck check box if already checked

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ernest1a
Forum Newbie
Posts: 13
Joined: Tue Dec 16, 2008 6:25 am

Uncheck check box if already checked

Post by ernest1a »

I don't know how to set a check box as unchecked if they are already checked and then store the value as unchecked. When user uncheck a checkbox and click Submit it should become as unchecked and store into table value "no". But it doesn't, it allways shows as checked and it doesn't story value "no". Please tell me what I should do.

The code blow is just an example of how I store the value of yes if check box is checked.

Code: Select all

 
<?php
if ($_POST[Nick]=='yes')
    {
    mysql_query ("UPDATE users SET privacy='yes' WHERE uID='Nick'");
    }
    else
    {
    mysql_query ("UPDATE users SET privacy='no' WHERE uID='Nick'");
    }
?>
 
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <table>
 
        <tr><td><input type='checkbox' name='Nick' value='yes'";
        <?php
        $result = mysql_query ("SELECT privacy FROM users WHERE uID = 'Nick'");
        $row = mysql_fetch_assoc($result);
        if ($row['privacy']=='yes')
        {
        echo "checked";
        }
        ?>"></td></tr>;
 
        <tr><td><input type="submit" value="Update"></td></tr>
    </table>
</form>
 
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Uncheck check box if already checked

Post by mickeyunderscore »

Try changing the code for the checkbox to:

Code: Select all

 
       $checked = '';
       $result = mysql_query ("SELECT privacy FROM users WHERE uID = 'Nick'");
       $row = mysql_fetch_assoc($result);
       if ($row['privacy']=='yes')
       {
       $checked = 'checked';
       }
       ?>
        <tr><td><input type='checkbox' name='Nick' value='yes' <?php echo $checked; ?> /></td></tr>;
 
ernest1a
Forum Newbie
Posts: 13
Joined: Tue Dec 16, 2008 6:25 am

Re: Uncheck check box if already checked

Post by ernest1a »

thanks, I tried but still the same. Everytime the value posted via $_POST[] is the same if I leave checked or if I uncheck. I have no idea why the value is passed if I uncheck checkbox and click Submit.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Uncheck check box if already checked

Post by mickeyunderscore »

That's very odd, when I run your code it sends the correct value via post. Perhaps it could be a browser thing, if you have any debugging plugins running you could try disabling them (I run firebug, and occassionally that causes some strange behaviour)
alexmila
Forum Newbie
Posts: 4
Joined: Sun Feb 01, 2009 12:37 pm

Re: Uncheck check box if already checked

Post by alexmila »

Do this in the first condition_

Code: Select all

 
 if (isset($_POST[Nick]) && $_POST[Nick]=='on')
{
   mysql_query ("UPDATE users SET privacy='yes' WHERE uID='Nick'");
     }
     else
    {  
   mysql_query ("UPDATE users SET privacy='no' WHERE uID='Nick'");
    }
 
If I remember and I'm not wrong, when you use check boxes only when they are selected they are passed through the browser. So isset asks if the variable was created. I added $_POST[Nick]=='on' which I think it's extra but It's valid, since if the variable was passed it will contain the value "on" not "yes".

I hope this helped.
Post Reply