![]() ![]() ![]() ![]() |
|||||
|
|||||
樓主 Alen ![]()
![]() |
連結範例是使用 onchange,可以是onload或其他網頁自動載入嗎(非手動)? http://www.w3schools.com/php/php_ajax_database.asp 請各位大大提供方法及說明,感激! |
1樓 |
http://www.w3schools.com/jsref/met_win_setinterval.asp
本篇文章回覆於2011-12-09 00:35
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
2樓 |
window.onload=function()
{ setTimeout(function() { //ajax call code here },5000);//延遲n毫秒 };
本篇文章回覆於2011-12-09 10:40
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
3樓
作者回應
Alen ![]() |
Solty大大您好:
首先感謝您以及蔥SIR的回答,第一次載入頁面的確是正常(自動載入),但將資料庫資料修改後,前端頁面還是無法更新資料,只能手動重整頁面。 附上前端及後端程式。 前端 <html> <head> <script type="text/javascript"> window.onload=function() { setTimeout(function() { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","getuser.php",true); xmlhttp.send(); },50);//延遲n毫秒5000 }; </script> </head> <body> <div id="txtHint"></div> </body> </html> 後端 <?php $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("peter", $con); $sql="SELECT * FROM user"; $result = mysql_query($sql); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> <th>Hometown</th> <th>Job</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "<td>" . $row['Hometown'] . "</td>"; echo "<td>" . $row['Job'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?>
本篇文章回覆於2011-12-09 19:24
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
4樓 |
那就用setInterval 阿
本篇文章回覆於2011-12-10 00:06
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
5樓
作者回應
Alen ![]() |
您是說加上setInterval,還是一樣喔!我還是要開後端getuser.php做重整才可以將前端資料更新。
var int=self.setInterval("showUser()",100); function showUser() { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","getuser.php",true); xmlhttp.send(); }
本篇文章回覆於2011-12-10 03:07
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
6樓
作者回應
Alen ![]() |
補充或刪除瀏覽紀錄。
本篇文章回覆於2011-12-10 03:20
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
7樓
最有價值解答
Solty ![]() |
xmlhttp.open("GET","getuser.php",true);<--這裡
改成 xmlhttp.open("GET","getuser.php?"+new Date().getTime(),true);
本篇文章回覆於2011-12-10 03:39
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
8樓
作者回應
Alen ![]() |
感謝Solty大大的臨門一腳,也感謝蔥SIR的寶貴資料。
本篇文章回覆於2011-12-10 08:41
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
9樓
作者回應
Alen ![]() |
想再請教如果我的後端程式,必須REQUEST前端的變數在select Mysql,請問該如何修改?
前端 var int=self.setInterval("showUser(1)",1000); function showUser(str) { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","getuser.php?q=1"+new Date().getTime(),true); xmlhttp.send(); } 後端 $q=$_REQUEST[["q"]; $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("peter", $con); $sql="SELECT * FROM user WHERE id = '".$q."'"; $result = mysql_query($sql); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> <th>Hometown</th> <th>Job</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "<td>" . $row['Hometown'] . "</td>"; echo "<td>" . $row['Job'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con);
本篇文章回覆於2011-12-10 15:41
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
10樓 |
xmlhttp.open("GET","getuser.php?q=1&t="+new Date().getTime(),true);
不要拿到魚就算了..也去了解一下為什麼要加上new Date().getTime() 簡單說,因為你是用get請求,所以相同url時,瀏覽器會cache住,getTime()會返回一串時間標記,每千分之一秒的值都不一樣,通過不斷變更url去欺騙瀏覽器 post就不會有這問題,另一種做法是在後端用header()宣告 no chche
本篇文章回覆於2011-12-10 16:41
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
11樓
作者回應
Alen ![]() |
Solty您好:
受教了!感謝您的指導。
本篇文章回覆於2011-12-10 22:41
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
回覆 |
如要回應,請先登入. |