Page 1 of 1

Uncheck check box if already checked

Posted: Sun Feb 01, 2009 8:20 am
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>
 

Re: Uncheck check box if already checked

Posted: Sun Feb 01, 2009 8:33 am
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>;
 

Re: Uncheck check box if already checked

Posted: Sun Feb 01, 2009 8:54 am
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.

Re: Uncheck check box if already checked

Posted: Sun Feb 01, 2009 9:06 am
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)

Re: Uncheck check box if already checked

Posted: Sun Feb 01, 2009 1:31 pm
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.