dongwu8064 2014-11-19 11:51
浏览 78
已采纳

如何单独回调ajax请求

I'm learning the way to work with ajax with php now i send request with;

    $.ajax({
        url: 'check_exists.php',
        type: "POST",
        data: registerForm.serialize(),
        success: function(data) {
            console.log(data)
            registerMsg = $('.registerMsg');
            if (data == 'nickname_exists') { 
                nickname.addClass('error'); 
            } else if (data == 'email_exists') { 
                email.addClass('error'); 
            } else {
                registerMsg.html(''); 
            }
        }
    });

Now this send to the php file and checks if data exists if i input nickname i get back nickname_exist back and if i do the same with the email i get email_exists back.

But now if i get both data it console.log like nickname_existsemail_exists this way it doesn't trigger the if statement.

i send from php file like;

require_once('db_connect.php');

if(isset($_POST['nickname'])){

    $nickname = $_POST['nickname'];

    $st = $db->prepare("SELECT * FROM users WHERE nickname=?");

    $st->bindParam(1, $nickname);
    $st->execute();

    if($st->rowCount() > 0) { echo 'nickname_exists';  }
}
if(isset($_POST['email'])){

    $email = $_POST['email'];

    $st = $db->prepare("SELECT * FROM users WHERE email=?");

    $st->bindParam(1, $email);
    $st->execute();

    if($st->rowCount() > 0) { echo 'email_exists';  }
}

How do i fix this, and is the way how i handle ajax to php the right way, can somebody help me a hand.

I need to make it console.log like

nickname_exists

email_exists

INSTEAD OF

nickname_existsemail_exists

  • 写回答

4条回答 默认 最新

  • douzhanshen0657 2014-11-19 11:57
    关注

    Collect your results into an array like this:

    require_once('db_connect.php');
    //Initalize the array
    $result = array(
        'nickname_exists' => 0,
        'email_exists' => 0
    );
    if(isset($_POST['nickname'])){
        $nickname = $_POST['nickname'];
        $st = $db->prepare("SELECT * FROM users WHERE nickname=?");
        $st->bindParam(1, $nickname);
        $st->execute();
        if($st->rowCount() > 0) { 
            $result['nickname_exists'] = 1;
        }
    }
    if(isset($_POST['email'])){
        $email = $_POST['email'];
        $st = $db->prepare("SELECT * FROM users WHERE email=?");
        $st->bindParam(1, $email);
        $st->execute();
        if($st->rowCount() > 0) { 
            $result['email_exists'] = 1;
        }
    }
    //Gives back the result in JSON.
    echo json_encode($result);
    

    Then return back with the json. After this you can check all of them in your javascript:

    //Initialize the hasError
    var hasError = false;
    if (data.nickname_exists == 1) {
        //Set error for nickname
        nickname.addClass('error');
        hasError = true;
    }
    if (data.email_exists == 1) {
        //Set error for email
        email.addClass('error');
        hasError = true;
    }
    //If we had no error:
    if (!hasError) {
        registerMsg.html('');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效