1. js-->php
a). JS Create Json
1<script>2$ (document). Ready (function(){3 /*--js Create json--*/4 varjsonobject={};//in another way:jsonobject={' name ': "Bruce", ' age ':--};5jsonobject[' name '] = "Bruce";6Jsonobject[' age '] = 25;7 Console.log (jsonobject);8Console.log (' This is stringfied JSON object: ' +json.stringify (Jsonobject));9 Console.log (Json.parse (json.stringify (Jsonobject)));Ten$ ("#demo"). HTML (jsonobject.name + "," +jsonobject.age); One /*--js Create json--*/ A - }); -</script>
Js code Create JSON array object
b). Pass Json from JS to PHP by using Ajax
1<script>2$ (document). Ready (function(){3 /*--js Create json--*/4 varjsonobject={};//in another way:jsonobject={' name ': "Bruce", ' age ':--};5jsonobject[' name '] = "Bruce";6Jsonobject[' age '] = 25;7 Console.log (jsonobject);8Console.log (' This is stringfied JSON object: ' +json.stringify (Jsonobject));9 Console.log (Json.parse (json.stringify (Jsonobject)));Ten$ ("#demo"). HTML (jsonobject.name + "," +jsonobject.age); One /*--js Create json--*/ A - /*--ajax Pass data to php--*/ - $.ajax ({ theURL: ' php/test.php ', -Type: ' POST ',//or use the type: ' GET ', then use $_get[' json '] or $_post[' json '] into PHP script - data: {json:JSON.stringify (Jsonobject)}, -Successfunction(response) { + Console.log (response); - varJsonobj =Json.parse (response); +$ ("#demo"). HTML ("from PHP ' s echo:" + Jsonobj.name + "," +jsonobj.age); A } at }); - /*--ajax Pass data to php--*/ - - }); -</script>
JS Side
1<script>2$ (document). Ready (function(){3 /*--js Create json--*/4 varjsonobject={};//in another way:jsonobject={' name ': "Bruce", ' age ':--};5jsonobject[' name '] = "Bruce";6Jsonobject[' age '] = 25;7Console.Log(jsonobject);8Console.Log(' This is stringfied JSON object: ' + JSON.stringify (Jsonobject));9Console.Log(Json.parse (JSON.stringify (Jsonobject));Ten$ ("#demo"). HTML (jsonobject.name + "," +jsonobject.Age ); One /*--js Create json--*/ A - /*--ajax Pass data to php--*/ -$.Ajax ({ theURL: ' php/test.php ', -Type: ' POST ',//or use the type: ' GET ', then use $_get[' json '] or $_post[' json '] into PHP script -Data: {json:JSON.stringify (Jsonobject)}, -Successfunction(response) { +Console.Log(response); - varJsonobj = JSON.Parse (response); +$ ("#demo"). HTML ("from PHP ' s echo:" + Jsonobj.name + "," + jsonobj.Age ); A } at }); - /*--ajax Pass data to php--*/ - - }); -</script>
PHP Side
2. Php-->js
a). PHP Create Json
1<?PHP2 3 $arr=Array(4' Name ' = ' Bruce ',5' Age ' = 25,6 );7 EchoJson_encode ($arr);//{"name": "Bruce", "Age":8 Echo $arr[' Name '];//Bruce9 EchoJson_decode (Json_encode ($arr))->{' name '};//BruceTen Echo implode((Array) Json_encode ($arr));//{"name": "Bruce", "Age": One A?>
PHP Code
b). PHP CURL call RESTful Web Service
Coming soon
3. Pass Json from PHP to PHP (must is array then Json_encode (' Json string ')?)
Http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page
4. Submit parameters to PHP through HTML form post/get to download a file (e.g. Excel ...)
I figure out a-a-around this. Instead of making a post call to force the browser to open the Save dialog, I'll make a post call to generate the file, Then temporary store the file on the server, return the filename. Then use a GET-call-for-this-file with "content-disposition:attachment; Filename=filename1 ". The GET call with the header would force the browser-to-open the "Save this file" dialog, always.
Code: Coming Soon
JSON is transferred between PHP and JS