我被困在这个ajax帖子里

时间:2015-11-07 06:39:36

标签: php jquery ajax

结果: enter image description here

这个ajax无法给我一个结果。我正在努力解决这个问题。我不知道为什么,我无法找到错误的位置。我尝试了另一个ajax并且它有效,但我不知道这个。怎么样?有人帮助我,谢谢。

ztest1.php:

<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <style></style>

    <script>
    function checkemail() {
        var status = document.getElementById("emailudahada");
        var u = document.getElementById("email").value;
        if (u != "") {
            document.getElementById("email").style.backgroundColor = "yellow";
            status.innerHTML = "<img src='/img/loading.GIF'></img>";
            $.ajax({
                url: '/ztest2.php',
                type: "POST",

                data: ({
                    email: u,
                    cekemailsignup: 'yes'
                }),
                success: function(result) {
                    status.innerHTML = result['result'] + "," + result['cekemailsignup'] + "," + result['email'];
                }
            });
        } else {
            document.getElementById("email").style.backgroundColor = "white";
        }
    }
    </script>
</head>

<body>
    <div id='emailudahada'></div>
    <input type='text' id='email' onblur='checkemail()'></input>
</body>

</html>

ztest2.php:

<?php
    include('ckcon.php');

    $cekemailsignup=isset($_REQUEST['cekemailsignup'])?$_REQUEST['cekemailsignup']:null;
    if($cekemailsignup=='yes'){
        $email=isset($_REQUEST['email'])?$_REQUEST['email']:null;
        $q=mysql_query("SELECT COUNT(email) AS ce FROM t_un WHERE email='$email' LIMIT 1");
        $f=mysql_fetch_object($q);
        $ce=$f->ce;
        if($email==null){
            $result="<img src='/img/xred.png'></img> <font color='red'>Cant be null value</font>";
        }
        if(strlen($email) < 4){
            $result="<img src='/img/xred.png'></img> <font color='red'>4 digit at minimum</font>";
        }
        if(is_numeric($email[0])){
            $result="<img src='/img/xred.png'></img> <font color='red'>1st character must be letter</font>";
        }
        if($ce<>0){
            //$result="<img src='/img/xred.png'></img> <font color='red'><strong>".$email."</strong> is taken</font>";
            $result="kampret lu";
        }
        echo "
            cekemailsignup=$cekemailsignup<br>
            email=$email<br>
            ce=$ce<br>
            result=$result<br>
        ";
        $ar = array(
                    'result' => $result,
                    'cekemailsignup' => $cekemailsignup,
                    'email' => $email
                    );
        echo json_encode($ar);
    }
?>

3 个答案:

答案 0 :(得分:1)

result字符串,要像对象一样使用它,需要将其解析为JSON。

var obj = JSON.parse(result);

您还可以在dataType: 'json',配置选项中设置$.ajax以默认设置,然后您不需要解析响应,可以直接使用。

由于jQuery包含在页面中,因此将其用于DOM操作。

完整代码:

$('#email').on('blur', function() {
  var $status = $('#emailudahada');
  email = $.trim($(this).val());

  if (email) {
    $(this).css('backgroundColor', 'yellow');
    $status.html('<img src=\'/img/loading.GIF\'></img>');
    $.ajax({
      url: '/ztest2.php',
      type: 'POST',
      dataType: 'json',

      data: ({
        email: email,
        cekemailsignup: 'yes'
      }),
      success: function(result) {
        $status.html(result.result + ',' + result.cekemailsignup. + ',' + result.email);
      }
    });
  } else {
    $(this).css('backgroundColor', 'white');
  }
});

答案 1 :(得分:1)

这里改变了js功能

<script>
function checkemail() {
    var status = document.getElementById("emailudahada");
    var u = document.getElementById("email").value;
    if (u != "") {
        document.getElementById("email").style.backgroundColor = "yellow";
        status.innerHTML = "<img src='/img/loading.GIF'></img>";
        $.ajax({
            url: '/ztest2.php',
            type: "POST",
            dataType: "json", //need to tell that response will as json
            data: ({
                email: u,
                cekemailsignup: 'yes'
            }),
            success: function(result) {
                status.innerHTML = result.result + "," + result.cekemailsignup. + "," + result.email;
            }
        });
    } else {
        document.getElementById("email").style.backgroundColor = "white";
    }
}
</script>

答案 2 :(得分:1)

HTML文件

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <style>
    </style>
    <script>
    function checkemail(){
        var status = document.getElementById("emailudahada");
        var u = document.getElementById("email").value;
        if(u != ""){
            document.getElementById("email").style.backgroundColor = "yellow";
            status.innerHTML = "<img src='/img/loading.GIF'></img>";
            $.ajax({
                url: '/ztest2.php',
                type: "POST",

                data: ({
                     email: u,
                     cekemailsignup: 'yes'
                }),
                success : 
                function(result2){
                	var result = JSON.parse(result2);
                    status.innerHTML=result['result']+","+result['cekemailsignup']+","+result['email'];
                }
            }); 
        }else{
            document.getElementById("email").style.backgroundColor = "white";
        }
    }
    </script>
</head>
<body>
    <div id='emailudahada'></div>
    <input type='text' id='email' onblur='checkemail()'></input>
</body>

PHP文件(ztest2.php)

<?php
    $ar = array(
                    'result' => "123",
                    'cekemailsignup' => "true",
                    'email' => "ririnputrian@gmail.com"
                    );
        echo json_encode($ar);
?>