首页 > 代码库 > ajax实现的点击数目加1代码实例

ajax实现的点击数目加1代码实例

ajax实现的点击数目加1代码实例:
点击按钮实现数字增加效果代码实例一章节中,介绍如何点击按钮实现数字加1的效果,但是好像并没有什么实际用处,下面就分享一段相对完整的能够在实际应用中派上用场的代码,此代码是ajax结合php代码实现的。
一.ajax代码如下:

<!DOCTYPE html><html><head><meta charset=" utf-8"><meta name="author" content="http://www.softwhy.com/" /><title>蚂蚁部落</title><script type="text/javascript">var xmlhttp=false;function add(){  try{    xmlhttp= new XMLHttpRequest;  }  catch(e){    xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");  }   xmlhttp.open(‘GET‘,‘count.php?a=a‘,false);  xmlhttp.onreadystatechange=func;  xmlhttp.send(null);} function func(){  if(xmlhttp.readyState==4){    var msg=xmlhttp.responseText;    var tt=document.getElementById("num");    tt.innerHTML=msg;  }}</script></head><body>当前页面数据库中访问次数:<div id=‘num‘></div><input type="button" value="http://www.mamicode.com/增加次数" ></body></html>

 二.php代码:

<?php  mysql_connect(‘localhost‘,‘root‘,‘‘);  mysql_selectdb(‘click‘);  $rs=mysql_query("UPDATE click SET num = num +1 WHERE name = ‘".$_GET[‘a‘]."‘");  if(mysql_affected_rows()==1){    $rs=mysql_query("select * from click where name=‘".$_GET[‘a‘]."‘");    $row=mysql_fetch_array($rs);    echo $row[‘num‘];  }?>

ajax实现的点击数目加1代码实例