[英]Update values from MYSQL table without reloading the page?
我為家庭項目構建了一個小型登錄系統,因此,當用戶登錄時,他將重定向到userspage.php
。在該頁面中,我從mysql表中獲取了一些數據,但是問題是當用戶按下這些按鈕時,我獲取的值將顯示給用戶,當用戶按下這些按鈕時,我需要執行php動作以增加該按鈕顯示的值(例如==> myvalue = 10 |當用戶單擊按鈕:myvalue = 11)。我已經構建了這一部分,它工作正常,但是我需要增加值並更新mysql列而不刷新頁面?我知道它的可能,但是每篇教程我嘗試過的網絡無法正常工作。
謝謝是前進!
AJAX是您要找的東西。 最好的庫是帶有$ .ajax()方法的jQuery http://api.jquery.com/jQuery.ajax/
以下代碼包含一個按鈕,該按鈕onclick調用updatedb.php,並在名為“ result”的div元素中顯示其輸出。
<html>
<head>
<script type="text/javascript">
function UpdateDb()
{
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("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","updatedb.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<button onclick='UpdateDb()'>Update</button>
<p> <span id="result"></span></p>
</body>
</html>
然后寫updatedb.php
<?php
do mysql update here.
and echo the result you want to display.
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.