簡體   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