这是我的javascript代码
<script>
$(document).ready(function(){
$(".five").click(function(){
<?php echo updatepoints();?>
});
});
</script>
这是我的PHP代码
<?php
function updatepoints() {
mysql_connect("localhost","user","password") or die (mysql_error());
mysql_select_db("database") or die ("Cannot connect to database");
$query = mysql_query("SELECT *from member WHERE username='" . $_SESSION["username"] . "'");
$row=mysql_fetch_array($query);
$points = $row["points"];
$points = $points + 5;
mysql_query("UPDATE member set points='" . $points . "' WHERE username='" . $_SESSION["username"] . "'");
}
?>
这两个代码都在同一页面下面。这似乎不会更新值。我该怎么办?
答案 0 :(得分:2)
在这里,试试这个:
旁注:content.php
将是您的SQL文件,由xmlhttp.open("GET","content.php",true);
调用。
这可行,假设您的SQL已经很好。
<!doctype html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
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("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","content.php",true);
xmlhttp.send();
}
</script>
</head>
</head>
<body>
<script>
setInterval(loadXMLDoc,5000);
</script>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
</body>
</html>
答案 1 :(得分:1)
创建一个名为ajax_update.php的文件。在该文件中添加你的函数和用户ajax来调用该函数。
ajax_update.php
<?php
function updatepoints() {
mysql_connect("localhost","user","password") or die (mysql_error());
mysql_select_db("database") or die ("Cannot connect to database");
$query = mysql_query("SELECT *from member WHERE username='" . $_SESSION["username"] . "'");
$row=mysql_fetch_array($query);
$points = $row["points"];
$points = $points + 5;
mysql_query("UPDATE member set points='" . $points . "' WHERE username='" . $_SESSION["username"] . "'");
}
if($_POST['action_type'] == 'update') {
updatepoints();
}
?>
像这样改变你的剧本
<script>
$(document).ready(function(){
$(".five").click(function(){
$.ajax({
type : "POST",
url : "ajax_update.php",
data : {action_type:"update"},
success : function(html){
}
});
});
});
</script>
您可以将ajax PHP文件更改为ajax和class的单独文件。在课堂上添加功能。让它整洁清晰
答案 2 :(得分:1)
你可以使用jQuery,它使事情更简单:
<script>
$(document).ready(function(){
$.post("update_user_points.php", { username: 'someuser' }, function(response) {
console.log(response);//check your console
});
});
</script>
我没有看到你如何启动会话,但是这个例子将username
值从客户端传递给PHP。如果你只是将点数增加5,你可以更好地检查这一点:
<?php
if(isset($_POST['username']){
function updatepoints($username) {
mysql_connect("localhost","user","password") or die (mysql_error());
mysql_select_db("database") or die ("Cannot connect to database");
$query = sprintf("UPDATE member set points = points + 5 WHERE username='%s'",
mysql_real_escape_string($username));
// Perform Query
$result = mysql_query($query);
// Check result
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}else{
echo 'update success';
}
}
//call the function
updatepoints($_POST['username']);
}
?>