返回ajax查询数据undefined

时间:2013-05-22 20:09:10

标签: php ajax jquery

您好我正在尝试创建一个ajax查询但是当我的结果返回时,我得到未定义作为响应。除了名为“hello”的对象,即使设置为“hello”,它也会返回“h”。我有一种感觉它与ajax发送数据的方式有关,但我对可能出现的问题感到迷茫。任何帮助将不胜感激。

这里是ajax

function doSearch() {

    var emailSearchText = $('#email').val();
    var keyCardSearchText = $('#keyCard').val();
    var userNameSearchText = $('#userName').val();
    var pinSearchText = $('#pin').val();
    var passwordSearchText = $('#password').val();


    $.ajax({
        url: 'process.php',
        type: 'POST',
        data: {
        "hello": "hello", 
        "emailtext": "emailSearchText", 
        "keycardtext": "keyCardSearchText", 
        "usernametext": "userNameSearchText",     
        "pinText": "pinSearchText", 
        "passwordtext": "passwordSearchText" 
        },
        dataType: "json",
        success: function (data) {
            alert(data.msg);
            var mydata = data.data_db;
            alert(mydata[0]);
        }
    });
}

然后这是php

include_once('connection.php');

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

        $hello = $_POST['hello'];
        $emailSearchText = mysql_real_escape_string($_POST['emailSearchText']);
        $keyCardSearchText = mysql_real_escape_string($_POST['keyCardSearchText']);
        $userNameSearchText = mysql_real_escape_string($_POST['userNameSearchText']);
        $pinSearchText = mysql_real_escape_string($_POST['pinSearchText']);
        $passwordSearchText = mysql_real_escape_string($_POST['passwordSearchText']);


        $query = "SELECT * FROM Students WHERE (`User name`='$userNameSearchText' OR `Email`='$emailSearchText' OR `Key Card`='$keyCardSearchText')AND(`Password`='$passwordSearchText'OR `Pin`='$pinSearchText')";
        $students = mysql_query($query);
        $count = (int) mysql_num_rows($students);
        $data =  array();

        while($student = mysql_fetch_assoc($students)) {
            $data[0] = $student['First Name'];
            $data[1] = $student['Last Name'];
            $data[2] = $student['Date of last class'];
            $data[3] = $student['Time of last class'];
            $data[4] = $student['Teacher of last class'];
            $data[5] = $student['Membership Type'];
            $data[6] = $student['Membership Expiration Date'];
            $data[7] = $student['Free Vouchers'];
            $data[8] = $student['Classes Attended'];
            $data[9] = $student['Classes From Pack Remaining'];
            $data[10] = $student['5 Class Packs Purchased'];
            $data[11] = $student['10 Class Packs Purchased'];
            $data[12] = $student['Basic Memberships Purchased'];
            $data[13] = $student['Unlimited Memberships Purchased'];
            $data[14] = $student['Groupon Purchased'];  
        };

        echo json_encode(array("data_db"=>$data, "msg" => "Ajax connected. The students table consist ".$count." rows data", "success" => true));                   


};

3 个答案:

答案 0 :(得分:4)

您的PHP脚本可能会生成错误消息,因为您尝试访问的$ _POST值与您在请求中发送的密钥名称不匹配。例如:$_POST['emailSearchText'],但您在AJAX调用中使用了emailtext

这很可能导致jQuery无法将响应解析为JSON,因此未定义。

答案 1 :(得分:1)

首先,您必须删除引号,否则您将传递这些文字而不是变量。

$.ajax({
    ...
    data: {
    hello: "hello", 
    emailtext: emailSearchText, 
    keycardtext: keyCardSearchText, 
    usernametext: userNameSearchText,     
    pinText: pinSearchText, 
    passwordtext: passwordSearchText 
    },
    ...
});

然后,就像ashicus指出的那样,在你的PHP文件中:

$emailSearchText = mysql_real_escape_string($_POST['emailtext']);
$keyCardSearchText = mysql_real_escape_string($_POST['keycardtext']);
$userNameSearchText = mysql_real_escape_string($_POST['usernametext']);
$pinSearchText = mysql_real_escape_string($_POST['pinText']);
$passwordSearchText = mysql_real_escape_string($_POST['passwordtext']);

答案 2 :(得分:0)

JS文件看起来没问题,所以这几条线索可以检查PHP文件。

  1. 看看那里是否有POST params(在php中全部是$ _POST)
  2. 检查你的偶数是否输入IF。添加一个else语句并回显一些虚拟json。
  3. 检查回显的编码json中的实际内容。将其分配给var然后打印。