无法使用jquery ajax从php接收数据

时间:2015-11-04 18:50:55

标签: php jquery ajax

我正在尝试检查我的mysql数据库中是否存在用户名,如果确实存在,那么php端会检查并回显“重复”,然后由ajax接收。但是它没有收到,当我尝试在我的开发人员工具上输出时,我将前端值视为未定义。

这是我的jquery ajax部分: -

$("#create").click(function(e){


    e.preventDefault();
   $.ajax({
        type:'post',
        url:'usernameTest.php',
        data: {usr: $("#usr").val(),
            pwd: CryptoJS.MD5($("#pwd").val()).toString()
        },
        success: function(data){
            //console.log(data);
            if(data == "duplicate"){
                $("#userErrorDiv").html("User already exists. Please enter another one").css("color","red");
                console.log("duplicate data");
            }
            else{
                console.log("data not duplicate:"+data);
            }
        },
        error(err){
        console.log("error "+err);
        }
    });

这是php部分: -

<?php

   function recieveFormData()
   {
     if (isset($_POST['usr'], $_POST['pwd'])) {
        global $connection;
        global $username;
        global $password;
        $username = $_POST['usr'];
        $password = $_POST['pwd'];

    }
  }

    /*do insertion if username not found */
    function insertIntoTable($username, $password){
        global $connection;
        $query="INSERT INTO users(username, password) VALUES('$username','$password')";
        $result=mysqli_query($connection,$query);

        if(!$result){
            die("Sorry. Query failed to execute ".mysqli_error());
        }

    }

    function connectToDatabase(){
        global $connection,$username;
        $connection=mysqli_connect("localhost","root","","usermanagement");
        if(!$connection){
            echo "Sorry! Cannot connect to the database";
        }
    }

    function readFromDatabase(){
        global $connection,$username;
        $query="SELECT * from users where username='$username'";

        $result=mysqli_query($connection,$query);
        if(!$result){
            die("query error");
        }
        if(mysqli_num_rows($result)!=0) {
            echo "duplicate";
            //return false;
        }
        else{
        while($row = mysqli_fetch_assoc($result)) {
           $usernameFromTable=$row['username'];
          // print_r($usernameFromTable);
           echo "<tr><td>".$usernameFromTable."</td><td>User</td><td></td></tr>";
        }
        }
        //return true;
    }

 recieveFormData();
 connectToDatabase();
 readFromDatabase();

 ?>

这是使用简单的html: -

 <label for="usr">Username</label>
<input type="text" name="usr" id="usr">
<label for="pwd">Password</label>
<input type="password" name="pwd" id="pwd">
<button id="create" class="create" type="submit" name="create">Create</button>
<div id="userErrorDiv"></div>

1 个答案:

答案 0 :(得分:1)

为了使答案更加明显,我将其添加到此处。

如果在收到Ajax请求的响应后,控制台记录您的回显字符串并将其与自身进行比较不起作用,请使用$.trim()

......
// Ajax execution
......
.success(function(response) {
    response = $.trim(response);
    if (response == 'expected response') {
        // Do stuff....
    }
});