Home  >  Article  >  Backend Development  >  php+ajax achieves increased page views and clicks

php+ajax achieves increased page views and clicks

墨辰丷
墨辰丷Original
2018-06-11 16:00:201589browse

This article shares with you a very practical code for adding 1 click views using php ajax. It is recommended to everyone. Friends in need can refer to it.

The following is a relatively complete code. Code that can come in handy in practical applications, this code is implemented by ajax combined with php code.

1.ajax code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>ajax实现浏览量点击增加</title>
<script type="text/javascript">
var xmlhttp=false;
function add(){
 try{
  xmlhttp= new XMLHttpRequest;
 }
 catch(e){
  xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
 }
 
 xmlhttp.open(&#39;GET&#39;,&#39;count.php?a=a&#39;,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>
当前页面数据库中访问次数:<p id=&#39;num&#39;></p>
<input type="button" value="增加次数" >
</body>
</html>

2.php code:

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

Summary: The above is the entire content of this article, I hope it can It will be helpful to everyone’s study.

Related recommendations:

How to use php for database reading, judgment and session login

File and form operations and database operations involved in php file upload

php common tips for error handling

The above is the detailed content of php+ajax achieves increased page views and clicks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn