【发布时间】:2014-04-19 10:14:55
【问题描述】:
div 的内容是由数据库按“cts”(计数)排序生成的。当我单击 div 时,我希望“cts”数字增加一,然后将更改更新到数据库。因此,内容会相应改变。 AJAX:
function addcount()
{
console.log('step1');
var id=this.rowid;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
console.log('step2');
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
console.log('step3');
document.getElementById("cts").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","addcount.php?q="+id,true);
xmlhttp.send();
}
php代码:
<?php
require_once "db.php";
session_start();
$id=intval($_GET['id'])
$sql="UPDATE words SET cts=cts+1 WHERE id='$id'";
if (!mysqli_query($con,$sql)){
die('Error: ' . mysqli_error($con));
}
?>
-------这根本不起作用-------提前谢谢你。
【问题讨论】:
-
为什么不
jQuery和jQuery.Ajax?看看这个:api.jquery.com/jQuery.post -
您的问题被标记为
jquery,但我没有看到您在代码中的任何地方使用 jQuery。你真的在用吗? -
如今的编程业务很简单。 1.堆积一些随机的代码行。 2.发现它不起作用。 3. 将其发布在 Stack Overflow 上,让他人为您整理。 4.??? 5. 利润!!!
-
旁注:您的代码成为The Horror of Implicit Globals 的牺牲品,因为您没有在
addcount函数中声明xmlhttp。总是声明你的变量。 -
您是否尝试过在不使用 Javascript/Ajax 的情况下构建它?这可能更容易调试(只是带有页面刷新的普通链接)。当它起作用时,添加 Ajax 应该是微不足道的。此外,如果出现问题,那么您就知道去哪里找了,因为服务器端部分已经好了。
标签: javascript php ajax database