The number of clicks implemented by ajax plus 1 code instance and ajax instance
The number of ajax-implemented clicks plus 1 code example:
In the "add effect code example" section, click the button to show you how to click the button to add a number to 1, but it seems to be of no practical use, next we will share a relatively complete piece of code that can be used in practical applications. This code is implemented using ajax and php code.
I. the ajax code is as follows:
<! DOCTYPE html>
Ii. 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']; }?>
Ajax instance code?
// Core object variable
Var xmlHttp;
// Differentiate browsers to create XMLHttpRequest core objects
Function create (){
If (window. XMLHttpRequset ){
XmlHttp = new XMLHttpRequest ();
} Else if (window. ActiveXObject ){
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
}
// Ajax core execution method (the plain text is returned after the request is submitted to the servlet for processing)
Function run (){
Create ();
Var value = document. getElementById ("id"). value;
Var URL = "/ajax/servlet/ajax? Name = "+ value;
XmlHttp. open ("GET", URL, true );
XmlHttp. onreadystatechange = callback;
XmlHttp. send (null );
}
// Callback function
Function callback (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
Var v = xmlHttp. responseText;
Alert (v );
}
}
}
Who has a simple AJAX code instance?
Js Section
// Generate the Ajax call object
Function creatAjax ()
{
Var HttpRequest = false;
Try {
HttpRequest = new XMLHttpRequest ();
} Catch (e ){
Var arrXMLHTTP = ["Msxml3.XMLHTTP", "Msxml2.XMLHTTP", "Microsoft. XMLHTTP"];
For (var I = 0; I <arrXMLHTTP. length; I ++ ){
Try {
HttpRequest = new ActiveXObject (arrXMLHTTP [I]);
} Catch (e ){}
If (HttpRequest) break;
}
}
Return HttpRequest;
}
// Use ajax
Function useAjax (username ){
Var ajax = creatAjax ();
StrUrl = "login. php"
Ajax. open ("POST", strUrl, false );
Ajax. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
StrPost = "username =" + username;
Ajax. send (strPost );
Var str = ajax. responseText;
If (str = 1 ){
Alert ("exist ");
} Else {
Alert ("nonexistent ");
}
Return false;
}
// PHP part
// Save some of the database connections
$ StrSql = "select count (*) as count from usertable where username = '". $ _ POST ["username"]. "'";
If ($ row ["count"]> 0 ){
Echo "1 ";
Exit;
} Else {
Echo "0 ";
Exit;
}