PHP jQuery utilized json format data operation, data transmission and reception of $ _POST

PHP jQuery utilized json format data operation, data transmission and reception of $ _POST

The first to recognize the jQuery syntax:
$ ( "# Sub") the Click (function () {.
$ .Post (data "to process data PHP page" to be transmitted (data can be a single, arrays, json format) , function (data) (callback) {
Alert (data); // call to the callback function for the module successfully processed, the output data returned
}, "json"); // if the callback function returns json format the data will have to add here "json" type of data returned, otherwise do not write
});

Then the data format recognized json:
{NAME1: VALUE1, NAME2: value2, ......}


Specific steps:
1, the html page from the page data to PHP:
(1) Code html page:
<INPUT ID = "buy_data1"> php // transmitted to the data page
<INPUT ID = "buy_data2">
<INPUT type = "submit" name = " sub" id = "sub" value = " see" the onClick = "look ()"> 
<span ID = "info">
to display the data returned from php
</ span>
(2) js Code:
function look ()
{
. $ ( "# Sub") the Click (function () {
. var buy_date1 = $ ( "# buy_date1") Val ();
var buy_date2 = $ ( "# buy_date2") .val ();
IF (buy_date1 == "" || buy_date2 == "")
{
Alert ( "incomplete information");
return;
}
$ .post ( "sum.php", {buy_date1: $ ( "#buy_date1").val(),buy_date2:$("#buy_date2").val()}, function(data){
alert(data);
},"json");
});
}
(3) php page code:
<PHP?
$ Buy_date = $ _POST [ 'buy_date1'];
$ buy_date2 = $ _POST [ 'buy_date2'];
echo 'Time 1:' $ buy_data1 'time of 2' $ buy_data2;...
summary:
click the "look" button, call the look () method, passing the data to json format $ buy_data1 and $ buy_data2 to php, if successful, the data is returned $ buy_data1 and $ buy_data2 and output to the front end.
2, the use of the callback function, receiving a return to the page from the PHP json data format, and to display the current page:

(1) php page code:
$ ARR = Array ( 'A' =>. 1, 'B' => 2, 'C' =>. 3, 'D' =>. 4, 'E' =>. 5);

json_encode echo ($ ARR); 
(2) JS Code:
function look ()
{
$ ( "# Sub") the Click (function () {.
$ .post ( "demo.php", function (Data) {
$ ( " the INFO1 # ") text (data.a);.
$ (" # INF02. ") text (data.b);
$ (" # Info3 ") text (data.c);.
}," JSON ");
} );
}
(. 3) HTML page code:
<span ID = "the INFO1">
</ span>
<span ID = "INF02">
</ span>
<span ID = "Info3">
</ span>
summary:
the PHP page the json format is to be noted, is: $ arr = array ( 'a ' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
output form: echo json_encode ($ arr); 

js portion
. $ ( "# the INFO1") text (data.a);
. $ ( "# INF02") text (data.b);
. $ ( "# Info3") text (data.c);
as the json back to the format of the data analysis and output to a respective tag which span

Guess you like

Origin www.cnblogs.com/fuhuo/p/11230795.html