【问题标题】:Update values from MYSQL table without reloading the page?从 MYSQL 表中更新值而不重新加载页面?
【发布时间】:2012-04-26 19:21:36
【问题描述】:

我为一个家庭项目构建了一个小型登录系统,所以,当用户登录时,他被重定向到userspage.php。在页面中我从我的 mysql 表中获取了一些数据,但问题是,我获取的值之一是向用户显示附近有一个按钮,当用户按下这些按钮时,我需要执行 php 操作来增加按钮显示的值(例如==> myvalue = 10 | 当用户点击按钮时:myvalue = 11我尝试过的每个网络教程都不起作用。

谢谢提前!

【问题讨论】:

  • 您需要为此使用 ajax。

标签: php jquery mysql html ajax


【解决方案1】:

AJAX 正是您所寻找的。最好的库是 jQuery,它的 $.ajax() 方法 http://api.jquery.com/jQuery.ajax/

【讨论】:

  • 只需包含 jquery 库并执行 onClick="$.ajax({url:'myscript.php?some=param&another=param'})"
  • 感谢您的帮助,但 Anil 为我提供了完美的代码!
【解决方案2】:

以下代码有一个按钮,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.
?>

【讨论】:

  • 你太棒了,代码对我来说完美!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 2013-01-24
  • 1970-01-01
  • 2019-07-10
  • 1970-01-01
相关资源
最近更新 更多