我是 JQuery 的新手,并试图创建一个计数器来计算点击次数并将数据库中的计数器更新一。我创建了一个按钮,单击该按钮我将计数器的值发送到数据库。并试图在我的第一页获取更新的计数。
我的代码是 -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Implementing counter</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<? session_start()?>
<? ob_start()?>
<?
if($_SERVER['HTTP_HOST']=='localhost'){
$host="localhost";
$user="root";
$pass="";
$dbas="db_name";
mysql_connect($host,$user,$pass,$dbas);
mysql_select_db($dbas);
}
?>
<script>
$(document).ready(function(){
$("#count").click(function(){
saveData();
$("#counter").load();
});
function saveData(){
$.ajax({
type: "POST",
url: "counter.php",
data: { count: "1"}
})
.done(function( count ) {
alert( "Counter Plus: " + count );
})
.fail(function() {
alert( "error" );
});
}
});
</script>
<br />
<button id="count">Add Counter</button><br>
<div id="counter">
<?
$fetch1=mysql_query("select `count` from user");
$fetch2=mysql_fetch_array($fetch1);
$counter=$fetch2['count'];
echo "You have ".$counter." Clicks";
?>
</div><br><br>
</body>
</html>
我的counter.php
样子——
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Counter</title>
</head>
<body>
<? session_start()?>
<? ob_start()?>
<?
if($_SERVER['HTTP_HOST']=='localhost'){
$host="localhost";
$user="root";
$pass="";
$dbas="db_name";
mysql_connect($host,$user,$pass,$dbas);
mysql_select_db($dbas);
}
?>
<?
if (isset($_POST['count'])) { // Form has been submitted.
$count = $_POST['count'];
$n=0;
$fetch1=mysql_query("select `count` from user");
$fetch2=mysql_fetch_array($fetch1);
$n1=$fetch2['count'];
$n=$n1+$count;
// INSERT THE DATA
$query = "UPDATE user SET `count`='{$n}'";
// Confirm if the query is successful.
$result = mysql_query($query);
echo "Counter Updated by 1";
}
?>
</body>
</html>
当我单击按钮时,counter.php 被调用,计数器被更新,但我< div >
的没有更新。counter.php 页面在我的屏幕上显示为一个对话框。
我的第一页&当我点击按钮时,它看起来像这样 -
告诉我怎么了??