Ajax Shopping Cart Knowledge Summary

Source: Internet
Author: User
Tags explode

Steps to run 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);

Session

You can store values in the Session object. The information stored in the session object is valid within the sessions and session scopes.

Example:

<% session ("username") = "Janine" session ("age") = 24%>
Pass value of Session:

Example:

$_session["UID"] = UID;
$_session["name"] = $PWDARR [1];//user name
$_session["account"] = $PWDARR [2];//user balance

Session Application:
Session_Start (); //must be used on the page header

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 implode, 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);

break the string into arrays: explode () function

Example:

<?PHP$STR = "Hello World". I Love shanghai! "; Print_r (Explode ("", $str));? >

To combine array elements into strings: implode () function
Example:
<?php$arr = Array (' Hello ', ' world! ', ' I ', ' love ', ' shanghai! '); echo Implode ("", $arr);? >

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);
}

Use of values 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) {}

A convenient way to convert a two-dimensional array to a one-dimensional array and then to a string:

Convert a two-dimensional array to a one-dimensional array
foreach ($spAttr as $v) {
$V =join (",", $v);
$arr [] = $V;

}
Convert a one-dimensional array to a string
Echo (Join ("^", $arr));
?>

Ajax Shopping Cart Knowledge Summary

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.