Hi,

I have a checkbox on a 'user info' page which is checked('Y') or unchecked('N') depending on the value received from my db.

# user.php
                    if ($row['9'] == 'Y') {
                         ?>
                         <input type="checkbox" name="approved" value="Y" checked onClick="this.form.action='user.php';this.form.submit()" />
                         <?
                    }
                    else {
                        ?>
                         <input type="checkbox" name="approved" value="Y" onClick="this.form.action='user.php';this.form.submit()" />
                        <?            
}

Now, I was wondering if someone could help me link this up with an UPDATE query in my database.

Basically I need an UPDATE query to change 'N' or 'Y' values.

IF checkbox is UNCHECKED and then changed to CHECKED {

UPDATE users WHERE id=$id VALUE = Y

}

-- OR --

IF checkbox is CHECKED and then changed to UNCHECKED {

UPDATE users WHERE id=$id VALUE = N

}

    $approved = (isset($_POST['approved'])) ? 'Y' : 'N'; // use $_GET if get method
    $sql = "UPDATE users SET approved='$approved' WHERE id=$id";
    // adjust column names to match actual names
    mysql_query($sql) or die(mysql_error());
    

      Brilliant NogDog. Once more you come to my rescue.

      So would placing this in the site work when the user ticks the checkbox. I'd prefer if they checked/unchecked rather than hit a submit button.

      Thank you once more.

        Hi NogDog,

        Can you tell me where I should place this on the 'user.php' script.

        Thanks for the assistance.

          8 days later

          Sorry for the bump, but I'm not sure how I should use this script that has been posted.

          Should I surround it with a if (isset($_POST)) { part?

          because for some reason my 'onClick="this.form.action='user.php';this.form.submit()"' doesn't submit 🙁

          I guess I just need that if the checkbox is checked/unchecked then the value is changed to 'Y' or 'N', depending on what it was previously.
          Use the update query and if it was 'Y', change to 'N' and vice versa.

          Thanks for any help.

            Hi,
            Set your action in the form tag. Like this..

            <form id="myform" name="myform" method="post" action="update.php">
              <input type="checkbox" name="box" id="box" onchange="myform.submit()" />
            </form>
            
              Write a Reply...