我找回這樣從我的數據庫中的數據:傳遞一個PHP字符串到JavaScript變量與阿賈克斯
while($row = mysql_fetch_array($result))
{
echo $row['uid'];
echo $row['note'];
}
,我和阿賈克斯打印這樣:
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText)
}
}
但問題是, uid並記下它的一個字符串。例如:
"45 note goes here"
如何分別打印每一行。事情是這樣的:
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log( <?php echo json_encode($row['uid']); ?> );
console.log( <?php echo json_encode($row['note']); ?> );
}
}
你是非常接近:用'mysql_fetch_array'收集數據到一個數組出它在一個'echo'與'json_encode' JSON。 – kirilloid 2012-03-14 09:42:04
我知道我真的很接近。我嘗試了很多方法,但我總是得到空。它應該是我的PHP中的東西。你可以請張貼關於如何的答案,因爲我幾乎試過了我想到的一切 – jQuerybeast 2012-03-14 09:43:15
@jQuerybeast「我嘗試了很多方法,但我總是得到空。」你檢查過你的MySQL結果嗎?你從數據庫中得到了什麼東西嗎?也許你的查詢是錯誤的或只是返回一個空的結果爲請求的參數... – feeela 2012-03-14 09:47:18