简体   繁体   English

如何使用jQuery检查ajax是否返回true或false值

[英]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... 我正在为我的网站构建一个注册表单,我使用jquery验证我的注册页面,当所有字段都以有效方式填充时,我想这样做,然后注册页面重定向或加载并将数据存储在数据库中...

1. first jquery code checks input fields 1.第一个jquery代码检查输入字段

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 2.然后在jquery代码中有一个ajax调用,它检查电子邮件是否已经存在,如果ajax调用返回true,则将数据插入数据库,如果返回false,则页面将不会加载并显示该电子邮件的消息已经存在

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. 我的代码中的问题是,当ajax调用为true或false时,我的页面会继续加载并在数据库中插入数据,但我希望如果value为false,页面将不会加载。

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 这是我的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: 您可以将async选项添加到false并在ajax调用之外返回:

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是否存在电子邮件,如果不存在则立即将其写入数据库:

email_validator.php 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: 我认为您应该调用PHP API方法将新电子邮件直接插入成功函数回调中:

    $.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
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery + localStorage - JSON 值检查真假 - jQuery + localStorage - JSON value check true or false Javascript / Jquery 将“no”的 cfquery 值返回为 false,将“yes”返回为 true - Javascript / Jquery returns cfquery value of 'no' as false and 'yes' as true 如何检查 mongodb.find 是否返回一些东西,或者不是那么真或假 - How to just check if the mongodb .find returns something or not so true or false 我如何不断检查我的 if 语句是否返回 true 或 false - How do I constantly check if my if statement returns true or false 我如何在我的jquery中检查php的返回值是“ true”还是“ false”? - How can I check in my jquery if the return value from php is 'true' or 'false'? 检查值是否为 false、true 或 null - Check if value is false, true or null 如何从javascript中的复选框中获取值以获取检查值是True还是False? - How to get value from checkbox in javascript for check value is True or False? 简单变量检查返回false,应为true - Simple variable check returns false, should be true 如何通过ajax发送复选框值:true或false - How to send checkbox value: either true or false, through ajax 如何在显示/隐藏按钮单击时使用 jQuery 在数据库表中存储布尔值真/假 - How to store a bool value true/false in database table using jQuery on show/hide button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM