Home  >  Article  >  Backend Development  >  Simple implementation of php and ajax

Simple implementation of php and ajax

巴扎黑
巴扎黑Original
2016-11-24 11:04:031226browse

Step 1. Create a new folder: js and put the latest jquery.js in it.

Step 2. Create a new file mygetjson.php, the content of which is


class custom_json {

        /** 
     * Convert array to javascript object/array 
     * @param array $array the array 
     * @return string 
    */                                                                    determine type
                                                                                                                                                                                                             ​); isset($array[$i]); ++$i) {
                                                                     $ OUTPUT. = Self :: _ Val ($ array [$ i]);
if ($ i! == $ last) $ OUTPUT. = ','; else {

                                             ’ each($array as $key => $value) {
                  $output .= '"'.$key.'":';                                                                                                                                                                                                           $output .=                                                 ($value);
                                                                                                                                                                                            return $output;

}

/** 
     * [INTERNAL] Format value 
     * @param mixed $val the value 
     * @return string 
    */
private static function _val($val)
{
if(is_string($val)) return '"'.rawurlencode($val).'" ';
          elseif(is_int($val)) return sprintf('%d', $val);
                                          using using using using using using       out out out out out out out out of ‐     ‐ ‐ ‐ ‐‐‐‐‐‐‐‐‐ ‐ val)) return ($val ? 'true' : 'false');
                                                                        ‐‐'''' return 'null'';
$big_test = array(
  array(
        'name' => array('John', 'Smith'),
                                                              using using   using using         ‐                                                                       > 180.53,
‘is_human’ => true,
'string' => 'Hello',
),
array(
' 'name' => array('Green', 'Alien'),
'age' => 642,
'sex' => null,
                                                                                                                                use           use using ’ s ’ s ’ s ​ ’ ​ to ​ to come ‐ to ​​
//echo custom_json:: encode($big_test);//This is used to test whether it returns json data
echo $_GET['jsoncallback'].'('.custom_json::encode($big_test).')';//Use a The callback returns
?>



Step 3. Create a new test page test.html with the content



Page Title
< meta charset ="utf-8">

                                                                               /javascript">
$(function(){
$('#btn').click(function(){



$.getJSON('json.php?jsoncallback=?',myfunction);
function myfunction(data)
{
var infoHtml = '';
$.each(data,function(contact,contactinfo)
{
// alert(contactinfo.name);

infoHtml+='

name:' +controlinfo.name+'& lt;/p & gt;';
$ ('#info'). html (infohtml);
}
}}}); & lt ;/head>









Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn