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