Home > Article > Backend Development > php+ajax achieves increased page views and clicks
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('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> 当前页面数据库中访问次数:<p id='num'></p> <input type="button" value="增加次数" > </body> </html>
2.php code:
<?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']; } ?>
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!