Php 是否在不重新加载页面的情况下更新MYSQL表中的值?
我已经为一个家庭项目构建了一个小型登录系统,因此,当用户登录时,他被重定向到Php 是否在不重新加载页面的情况下更新MYSQL表中的值?,php,jquery,mysql,html,ajax,Php,Jquery,Mysql,Html,Ajax,我已经为一个家庭项目构建了一个小型登录系统,因此,当用户登录时,他被重定向到userspage.php。在页面中,我从mysql表中获取了一些数据,但问题是,我获取的一个值显示给用户,旁边有一个按钮,当用户按下这些按钮时,我需要执行一个php操作来增加按钮显示的值(例如==>myvalue=10 |当用户点击按钮时:myvalue=11)。我已经构建了这个部分,它工作得很好,但是我需要在不刷新页面的情况下增加值并更新mysql列?我知道这是可能的,但我尝试过的每个web教程都不起作用 感谢是前进
userspage.php
。在页面中,我从mysql表中获取了一些数据,但问题是,我获取的一个值显示给用户,旁边有一个按钮,当用户按下这些按钮时,我需要执行一个php操作来增加按钮显示的值(例如==>myvalue=10 |当用户点击按钮时:myvalue=11)。我已经构建了这个部分,它工作得很好,但是我需要在不刷新页面的情况下增加值并更新mysql列?我知道这是可能的,但我尝试过的每个web教程都不起作用
感谢是前进 AJAX是您需要的。最好的库是jQuery,它的$.ajax()方法就是ajax。最好的库是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()
{
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“结果”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“updatedb.php”,true);
xmlhttp.send();
}
更新
然后编写updatedb.php
<?php
do mysql update here.
and echo the result you want to display.
?>
下面的代码有一个按钮,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()
{
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“结果”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“updatedb.php”,true);
xmlhttp.send();
}
更新
然后编写updatedb.php
<?php
do mysql update here.
and echo the result you want to display.
?>
这需要使用ajax。这需要使用ajax。只需包含jquery库并执行onClick=“$.ajax({url:'myscript.php?some=param&other=param'))谢谢您的帮助,但Anil为我提供了完美的代码!只需包含jquery库并执行onClick=“$.ajax({url:'myscript.php?some=param&other=param'})”感谢您的帮助,但Anil为我提供了完美的代码!伙计,你太不可思议了,这个代码对我来说太完美了!非常感谢!伙计,你太不可思议了,这个代码对我来说太完美了!非常感谢!