Ajax writing a shopping cart encounter problems

Source: Internet
Author: User

Use Hyper global variable $_session[]

1. Open at the beginning of the file: Session_Start ();

2.$_session[] The output is an array

3.$_session[] exists in the cache, clearing the cache clears the data

4. Can be used to store commonly used variables, get the way to $_session["key Name"]

Verify Login

Use $_session[] To determine whether the login pass value is empty, if empty, the login page is recalled.

<?php
Session_Start ();
if ($_session["uid"] = = "") {
Header ("location:login.php");
}
?>

When you call the same method multiple times, you can encapsulate the method and then call the file to

JS Common format

Page initialization

Window.onload = function () {
Initialize page
Inithtml ();
}

function inithtml () {
Creating objects
var xhr=new xmlhttprequest ();
Initiating a request
Xhr.onreadystatechange =function () {
if (xhr.readystate==4) {
var str = Xhr.responsetext;
var arr=str.split ("@");

Assigning values from Ajax to methods
Table (arr[1]);
P (arr[0]);

}
}
Xhr.open ("Post", "php/index_php.php");
Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');
Send data
Xhr.send ("Type=init");
}

Issue: Post pass value background accept null value

1. Missing Write header file

Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');

2. The value of the passed variable is not correctly obtained (check how to get the variable)

3. Error in background accept mode (receive with post value with Get)

Common methods

1.php Array goto string

Convert a two-dimensional array into a string

Session_Start ();
$arr =$_session["Data"];
if (empty ($arr)) {
echo "";
}else{
foreach ($arr as $k = = $v) {
$attr []=join (",", $v);
}
echo Join ("^", $attr);
}

2.js converting a string into a two-dimensional array

function Strtoarr (STR,FG1,FG2) {
Turn a string into a two-dimensional array
var temp = Str.split (FG1),
arr = [];

for (var i = 0; i<temp.length;i++) {
Arr[i] = Temp[i].split (FG2);
}
Returns a two-dimensional array
return arr;
}

Note:

var dianji=prompt ("Enter purchase Quantity");

The prompt comes with a return value that can be used directly

Array_splice ($arr, $k, 1);

Destroys elements from the array starting with the number of K subscript 1

String Stitching Tips

First use "" to split the preceding string, then add + +

Example: "Name variable Age variable"

"Name" + variable + "age" + variable

When a page needs to process multiple Ajax

Pass a custom attribute to distinguish each Ajax passed over

Ajax writing Shopping cart problems

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.