The number of clicks implemented by ajax plus 1 code instance and ajax instance

Source: Internet
Author: User

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;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.