Ajaxphp implementation of passing and receiving variables and code _ php instance

Source: Internet
Author: User
In practical applications, this situation occurs: variables are transmitted and received between ajax and php. to implement this function, many new friends can only sigh with hope, so I collected some practical skills and shared them with you, hoping to help you So, your jQuery might be something like .....

The code is as follows:


$. Ajax ({
Url: 'query. php ',
Data: {id: 10 },
Datatype: json
Success: function (results ){
If (results. msg = 'success '){
For (var I in data ){
$ ('# Content'). append (
'Id = '+ results. data [I]. id + ', description =' + results. data [I]. description + ', msrp =' + results. data [I]. msrp
);
}
} Else {
$ ('# Content'). append (results. msg );
}
}
});


And your php ....

The code is as follows:


If (isset ($ _ GET ['id']) {
$ SQL = "SELECT id, description, msrp FROM tbl WHERE id = '{$ _ GET ['id']}'";
$ Return = array ();
If ($ result = mysql_query ($ SQL )){
If (mysql_num_rows ($ result )){
$ Return ['MSG '] = 'success ';
While ($ row = mysql_fetch_assoc ($ result )){
$ Return ['data'] [] = $ row;
}
} Else {
$ Return ['MSG '] = 'no results found ';
} Else {
$ Return ['MSG '] = 'query failed ';
}
Header ("Content-type: application/json ");
Echo json_encode ($ result );
}

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.