0
我修改了代碼在這裏(http://www.w3schools.com/PHP/php_ajax_database.asp)更新和顯示我的數據庫後,輸入框鍵入並單擊按鈕提交。 我Ajax更新數據庫後提交
<form method="post" action="process.php" id="processer" onsubmit="showUser('me');">
<label for="Process">Process</label><input type="text" name="process" id="process">
<input type="submit" value="Process">
</form>
我的Ajax代碼發送:
xmlhttp.open("GET","getuser.php?q="+str+"&t=" + Math.random(),true);
xmlhttp.send();
最後我getuser.php是
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("short", $con);
$sql="SELECT * FROM table WHERE creator = '$q'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>id</th>
<th>data</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['data'] . "</td>
</tr>";
}
echo "</table>";
mysql_close($con);
?>
的問題是,當我點擊提交,有時1次刷新,有時會刷新2次,其他則根本沒有。假設我正在提交數字1到10,第一次通過我看到1,2,然後輸入3後,我什麼都看不到,然後突然3,4,然後什麼也沒有5(當我剛剛輸入6) ,然後是6,然後是7,8.
我的php顯示數據是否被緩存?或者我怎樣才能使清爽流暢和一致?
在此先感謝。
您的示例代碼易受SQL漏洞攻擊。 (就像w3schools上的所有例子一樣,你會在其他地方找到更好的AJAX介紹)。 - 我不知道1,2,3,4是什麼意思,但是不要用繁瑣的手動xmlhttprequest設置,而是使用jQuery來更可靠地處理潛在的緩存問題。 – mario 2011-05-24 21:27:08
非常感謝您,事實證明我在將數據存儲到數據庫之前太快地調用了數據 – 2011-05-24 21:36:39