Home > front end >  jquery variable won't change to false in php even though in the console is changing
jquery variable won't change to false in php even though in the console is changing

Time:06-11

I just encountered a weird bug.

I'm checking if a checkbos is checked or not using JQUERY-AJAX and then i'm sending the value ( true of false ) to my php file.

I'm checking the console and when the checkbox is ticked , the value of my variable "preferEmail" is changing to "true" and vice versa .

But then in PHP , it always sees it as ticked, even when it's not , and i can see in the console that the JQUERY validation is working fine.

The weird part is that it worked fine a couple of times, and then it stopped working and i haven't touched the code.

I need the value of $errorEmptyPreferEmail to change to "true", when $preferEmail is false , but it always stays false.

<form id="contact-form" method="post" action="../php/contact.php"> 
<input type="checkbox" id="mail-repair" name="email-me" value="prefer-email">
<button id="mail-submit" type="submit" name="submit" >SEND</button>
</form>

 // everything is working fine here , "prefer-email" is the name of my input type="checkbox"

$(document).ready(function () {
        $("#contact-form").submit(function (event) {
            event.preventDefault();
 
 var preferEmail = false;   // initialising the checkbox as false ( meaning not checked)
 if($('input[name="prefer-email"]').is(':checked')){  
 // if this condition is met then i change the value of preferEmail to true
                  preferEmail = true;
                  console.log(preferEmail);  // checking if the code is running properly
              }
               else{
                  console.log(preferEmail); 
              }       

 $.ajax({
url: "../php/contact.php",
type: "POST",

data: {                           
                preferEmail: preferEmail,
                submit: submit},
success: function (event) {
    $('#hidden').html(event);      }
});

   });
    });

   

<?php
if(isset($_POST['submit'])){
$preferEmail = $_POST['preferEmail'];
$errorEmptyPreferEmail = false;
if ( $preferEmail != true ) {
    $errorEmptyPreferEmail = true;
    echo "<script type='text/javascript'>alert('validated');</script>";
}
}

If in my php file I do :

elseif ( $preferEmail == true ) {
    $errorEmptyPreferEmail = true;
    echo "<script type='text/javascript'>alert('validated');</script>";
}

The alert always pops up , even when $preferEmail is false in the console .

How do I fix this ?

CodePudding user response:

the ajax send true or false to PHP as a string type.

method_1 : in PHP you could change true to "true" in conditions like this

if(isset($_POST['submit'])){
$preferEmail = $_POST['preferEmail'];

$errorEmptyPreferEmail = false;
if ( $preferEmail !== "true" ) {

    $errorEmptyPreferEmail = true;
    echo "<script type='text/javascript'>alert('not validated');   </script>";
  }elseif ( $preferEmail === "true" ) {
    $errorEmptyPreferEmail = false;
    echo "<script type='text/javascript'>alert('validated');</script>";}}

or method_2: first convert your data into json type

$.ajax({
url: "res.php",
type: "POST",

data: {                           
                preferEmail: JSON.stringify(preferEmail),
                submit:JSON.stringify(submit)
               
            
            },

and decode them in PHP :

$submit = json_decode($_POST['submit']);
$preferEmail =  json_decode($_POST['preferEmail']);
  • Related