1

I have a php function which updates email only if checkbox is checked. However, it always execute if statement even if the checkbox value is false.

I've set if ($_POST['emailCheckbox']) to only execute query if checkbox is checked, but for some reason it executes every time even if the value is false.

This is value of $_POST array when checkbox is not ticked. Otherwise it prints true.

Array ( [emailContact] => [email protected] [passwordContact] => password [firstNameContact] => John [emailCheckbox] => false )

$error = "";

        if (!$_POST['passwordContact']) {

            $error = "<p>You need to provide your password to make changes.</p>";
        }

        if ($error != "") {
            echo $error;
            exit();

        } 

        $query = "SELECT * FROM users WHERE id=".$_SESSION['id']." LIMIT 1";
        $result = mysqli_query($link, $query);

        if (mysqli_num_rows($result) > 0) {

            $row = mysqli_fetch_assoc($result);

            if (!password_verify($_POST['passwordContact'], $row['password'])) {

                $error = "<p>Your password is incorrect. Please try again.</p>";

            } else if ($_POST['emailContact'] == "") {

                $error = "<p>Enter new email address.</p>";

            } else if (filter_var($_POST['emailContact'], FILTER_VALIDATE_EMAIL) == false) {

                $error = "<p>Please enter a valid email address.</p>";

            } else {

                if ($_POST['emailCheckbox']) {

                    $queryEmailCheck = "SELECT * FROM users WHERE email = '".mysqli_real_escape_string($link, $_POST['emailContact'])."' LIMIT 1";
                    $resultEmailCheck = mysqli_query($link, $queryEmailCheck);

                    if (mysqli_num_rows($resultEmailCheck) > 0) {
                        $error = "That email address has already been taken.";
                    }

                }

            }

            if ($error != "") {
                echo $error;
                exit();

            } else {

                if ($_POST['emailCheckbox']) {

                    $queryUpdate = "UPDATE users SET email='".mysqli_real_escape_string($link, $_POST['emailContact'])."', firstName='".mysqli_real_escape_string($link, $_POST['firstNameContact'])."' LIMIT 1";

                } else {

                    $queryUpdate = "UPDATE users SET firstName='".mysqli_real_escape_string($link, $_POST['firstNameContact'])."' WHERE id='".$_SESSION['id']."' LIMIT 1";

                }


                print_r($_POST); //For testing purposes.
                //mysqli_query($link, $queryUpdate);

                echo 1;
            }

        }

In my AJAX call I have this code:

$.ajax({
                    type: "POST",
                    url: "actions.php?action=editMyContactDetails",
                    data: "emailContact=" + $("#emailContact").val() + "&passwordContact=" + $("#passwordContact").val() + "&firstNameContact=" + $("#firstNameContact").val() + "&emailCheckbox=" + $("#editEmailCheckbox").is(":checked"),
                    success: function(result) {
                        if (result == "1") {
                            console.log(result);
                        } else {

                            $("#editContactAlert").html(result).show();
                        }
                    }
                })

How can I ensure that if ($_POST['emailCheckbox']) execute only when checkbox value is true?

2 Answers 2

1

$_POST variables are strings, and all strings evaluate to true except empty string "" and string "0". So compare with a string:

if ($_POST['emailCheckbox'] == 'true') {

Or validate some strings as boolean (see Validate Filters). Here string "true" evaluates to boolean true and string "false" evaluates to boolean false:

if (filter_var($_POST['emailCheckbox'], FILTER_VALIDATE_BOOLEAN)) {

Alternately, you could NOT pass anything if NOT checked and pass something (true, 1, etc.) if checked and then check if it is set, as checkboxes that are not checked will not be passed in the $_POST array:

if (isset($_POST['emailCheckbox'])) {
Sign up to request clarification or add additional context in comments.

Comments

0

You're checking if the parameter is passed (it is, always) thus it isn't testing if the value is "true" or "false"

e.g. you need to check the value that is passed

if($_POST['emailCheckbox'] == "true"){...}

Comments

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.