2014-04-15 31 views
0

我一直試圖弄清楚這一整天,我打磚牆(與我的頭)。

我有這個網站:

<!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的沒有問題。 感謝您的幫助。 格雷厄姆

+0

如果你得到後面的字符串,即「某些東西」,這意味着你的查詢不返回任何行。如果它返回任何行,請首先檢查您的查詢 –

+0

我不知道「connect.php」包含的是什麼,但我會假設它只生成數據庫連接。所以我的猜測是'$ username'是未定義的。 – jeroen

+0

'$ username = mysql_real_escape_string($ username);'最初設置'$ username'在哪裏? –

回答

2

你的心不是查詢返回任何結果,下面可能是因爲:

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

應該

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

因爲沒有i日期形式輸入。

此外,$username似乎並未設置在任何地方。

+0

感謝大家指出$ username沒有設置。我已經包含'$ username = $ _COOKIE ['visit'];'和'imageDate'改變,現在一切都很好。唷! –

相關問題