简体   繁体   中英

how to check that ajax returns true or false value using jquery

i'm building a signup form for my website i validate my signup page with jquery i want to do that when all the fields are filled with valid way then the signup page redirect or load and store the data in database...

1. first jquery code checks input fields

2. then in the jquery code there is a ajax call which check if the email already exist or not if the ajax call return true then data will be inserted in database if it return false the the page will not load and display the message that email is already exist

the problem in my code is that when the ajax call is true or false my page keep loading and insert the data in database but i want that if value is false page will not load.

here is my code

 $.ajax({
      type:'POST',
      url:'email_validator.php',
      data:{email:mail},
      success:function (response){
         var result = $.parseJSON(response);
          if (result.status===false) {
              $('#error').html("Email alreaday exist");
              return false;
            } if(result.status===true) {
               return true;
            }
      }
   });

and here is my email_validator.php

<?php   
      if(isset($_POST["email"])){
            $result = array();
            $email=$_POST["email"];
            $db=new PDO("mysql:host=localhost;dbname=the_scops","root","");
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $STH=$db->prepare("SELECT email FROM signup WHERE email=?");
            $STH->execute([$email]);
            if($STH->rowCount() == 1){
                //echo "Email alreday exist";
                 $result["status"] = false;
            }
            else{
                $result["status"] = true;
            }
              echo json_encode($result);
            exit(0);
      }

You can add async option to false and return outside the ajax call:

function testAjax() {
   var resultStatus = "";

   $.ajax({
      type:'POST',
      url:'email_validator.php',
      async: false,
      data:{email:mail},
      success:function (response){
         var result = $.parseJSON(response);
         resultStatus = result.status;

         if (result.status===false) {
             $('#error').html("Email alreaday exist");
         }
      }
   });

   return resultStatus;
}

[UPDATE]

But you do not need to check this on client side. Yo can check if email exists in your email_validator.php and immediately write it to the database if it does not exist:

email_validator.php

<?php   
      if(isset($_POST["email"])){
            $result = array();
            $email=$_POST["email"];
            $db=new PDO("mysql:host=localhost;dbname=the_scops","root","");
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $STH=$db->prepare("SELECT email FROM signup WHERE email=?");
            $STH->execute([$email]);
            if($STH->rowCount() == 1){
                //echo "Email alreday exist";
                 $result["status"] = false;
            }
            else{
                $result["status"] = true;

                // --> INSERT the new email into the database
            }
              echo json_encode($result);
            exit(0);
      }

Try this:

$.ajax({
    type:'POST',
    url:'email_validator.php',
    data:{email:mail},
    success:function (response){
        if (response.status === true) {
            return true;
        } else {
            $('#error').html("Email alreaday exist");
            return false;
        }
    }
});

In my opinion you should invoke the PHP API method to insert the new email directly inside the success function callback:

    $.ajax({
        type: 'POST',
        url: 'email_validator.php',
        data: {email: mail},
        success: function (response) {
            var result = $.parseJSON(response);

            if (result.status === false) {
                $('#error').html("Email already exists");
            }
            else (result.status === true) {
                // -> Call here the PHP api method to insert the new email in the database
            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM