Basic knowledge of Ajax

Source: Internet
Author: User

The basic format of Ajax

var info= "";
Creating objects
var xhr=new xmlhttprequest ();
Handling Return Values
Xhr.onreadystatechange = function () {

Determine if loading is complete
if (xhr.readystate = = 4) {
if (xhr.responsetext== "success") {
Location.href= "index.php";
}else{
Alert ("Login" +xhr.responsetext);
}
}
}
Initiating a request
info= "uid=" +uid+ "&pwd=" +PWD;

Get Pass Value
Xhr.open ("Get", "php/login_php.php?") +info);

Post Pass Value
Xhr.open ("Post", "php/login_php.php");

Get Value Header text
Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');
Send data
Xhr.send (info);

String conversion Issues

Converts a two-dimensional array that is queried in the data to a string

Querying the database

$sql = "SELECT * from Fruit";

Execute SQL statement
$ret = $db->query ($sql);

Convert the result to a two-dimensional array
$SPARR = $ret-Fetch_all ();
Convert a two-dimensional array to a string
$str = "";
foreach ($spArr as $v) {

Traverse two-dimensional array, get the one-dimensional array $v, divide the one-dimensional array with Inplode, the result is added to itself, and each one-dimensional array is divided by ^
$str. =implode (",", $v). " ^";
}

Remove the excess ^
Echo substr ($str, 0,-1);

Convert a string to a two-dimensional array

Splitting a string into a one-dimensional array with split and delimiter ^

var temp=str.split ("^");
var arr=[];

Loop to add data from a one-dimensional array to an array
for (Var i=0;i<temp.length;i++) {
Arr[i]=temp[i].split (",");
Console.log (arr);
}

The use of value-passing in Ajax

Get the value of the PHP page when it is finished processing

var str = Xhr.responsetext;
Show (str);

Define a parameter to accept the value passed over

function Tshow (str) {}

Basic knowledge of Ajax

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.