繁体   English   中英

有人可以告诉我为什么我的$ .ajax调用不从我的PHP页面返回数组吗?

[英]Can someone tell me why my $.ajax call isn't returning the array from my PHP page?

我整天都在努力解决这个问题,而且正在撞墙(用我的头)。

我有这个HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Construct Films - Interface</title>
    <script src="jQuery.js"></script>
    <script>
    $(document).ready(function(){
        var oXHR; 
        $("#timeSel").submit(function(event){
            if (oXHR){
                oXHR.abort();
            }
            var $form = $(this);
            var $inputs = $form.find("input, select, button, textarea");
            var serializedData = $form.serialize();
            $inputs.prop("disabled", true);
            var iDate = $("input#imageDate").val();
               oXHR = $.ajax({
                    url: "fetch_images.php",
                    type: "post",
                    data: serializedData
                });
            oXHR.done(function (response, textStatus, jqXHR){
                // log a message to the console
                $("#output").text(response);
                console.log("Hooray, it worked!");
                });
            oXHR.fail(function (jqXHR, textStatus, errorThrown){
                // log the error to the console
                console.error("The following error occured: "+textStatus, errorThrown);
                });
            oXHR.always(function () {
                // reenable the inputs
                $inputs.prop("disabled", false);
                });
        event.preventDefault();
        });
    });
    </script>
</head>
<body>
    <div id='usrSelection'>
        <form id="timeSel">
            Select date: <input id="imageDate" name="imageDate" type="date">
            <input type="submit" value="Send">
        </form>
    <div id='output'></div>
</body>
</html>

这是我的PHP:

<?php
    include 'connect.php';
    $username  = mysql_real_escape_string($username);
    $imageDate = mysql_real_escape_string($_POST['iDate']);
    $result = mysql_query("
        SELECT
            *
        FROM
            images AS i
        INNER JOIN
            users AS u ON i.userID = u.UserID
        WHERE
            u.username = '$username'
        AND 
            i.imageDate = '$imageDate'
        ORDER BY
        imageTime
        ASC
    ") or die(mysql_error());
    // populate the imageSelector div with a time selector
    while ($row = mysql_fetch_assoc($result)) { 
        echo $row['imageTime'];
        //echo "<option>" . $row['imageTime'] . "</option>";
    }
?>

我在控制台中收到消息“万岁,它起作用了!” 但是我没有从MySQL查询或SQL错误中获取任何实际数据。我是AJAX的新手,试图弄清楚为什么它不起作用。 如果我将php页面更改为类似

<?php
echo "some stuff";
>

然后我得到返回到输出div没问题。 谢谢你的帮助。 格雷厄姆

您的查询未返回任何结果,可能是由于以下原因:

$imageDate = mysql_real_escape_string($_POST['iDate']);

应该

$imageDate = mysql_real_escape_string($_POST['imageDate']);

因为没有iDate表单输入。

另外, $username似乎没有在任何地方设置。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM