In this case, we use Ajax to pass values to a PHP page and process the resulting array through the Json_encode () function, and then return to Ajax, which is part of the code that is extracted in the actual case:
PHP page
Public Function Showchatname () { $chat = A (' article ', ' Event '); $res = $chat->selectname (I (' Get.channel ')); echo Json_encode ($res); }
The output form of array $res after json_encode processing
{"Channel_name": "ABC"}
Template page
<script src= ". /.. /public/home/js/jquery.min.js "></script> <script type=" Text/javascript ">//Get Cookie value funct Ion GetCookie (name) {var arr,reg=new RegExp ("(^|)" +name+ "= ([^;] *)(;|$)"); if (Arr=document.cookie.match (reg)) {return unescape (arr[2]); }else{return null; }} $ (function () {var channel = GetCookie (' channels '); var a = Window.location.host; $.ajax ({type: ' Get ',//' get ' with background communication URL: '/http '/' +a+ '/article/showchatname ',//communicate with this PHP page DataType: ' json ',//The value returned from PHP interprets data in JSON form: ' channel= ' +channel, Cache:false, Success:function (msg) {//If the call to PHP succeeds, note that MSG is the object returned, this you can customize if (msg.channel_name! = ") {//channel_nam E is a key value of the array var b = msg.channel_name; $ ("H1"). Text (b); ReTurn false; } } }); }); </script> <body> <div id= "Headlinecontainer" >   ;
Use $.cookie (' channels ') when prompted: Object doesn ' t support property or method ' cookie '! Do not know what is going on, all of them with JS wrote a method to obtain a cookie, and then directly called.
Click here to download the demo version