Home  >  Article  >  php教程  >  jQuery/Ajax/PHP/Json 的一个综合例子

jQuery/Ajax/PHP/Json 的一个综合例子

WBOY
WBOYOriginal
2016-06-13 10:47:361434browse

jQuery 是一个优秀的 Javascript 框架,对 js 进行了优秀的包装,提供了许多方便的功能。jQuery 对 ajax 的包装也堪称优秀。 
 
    jQuery 可以以 json 文件传输协议来传输数据(类似 xml,而且大有取代 xml 的趋势),而网站后台代码必须与之配合使用。PHP 是用 json_encode 函数来对返回的数组数据进行编码的,但这个函数只有 PHP5.2版本以上才支持。 
 
    从网上找到一个 json 的操作类,本人在 PHP4.4.7 版本下测试通过。本人还建了个函数 function my_json_encode($phparr),使代码兼容 PHP5.2 以上版本。 
 
    示例代码(包括 json 的类包软件)可以在以下网址下载:http://www.BkJia.com/uploadfile/2012/0221/20120221090101730.rar
  
 
    以下是全部代码: 
 
 

 
jQuery Ajax 实例演示 
 
 
 
 
 
 
 
   

输入姓名:

 
   

输入年龄:

 
   

输入性别:

 
   

输入工作:

 
 
 
 
 
 
 
 
 
 
PHP 文件 ajax_json.php: 
 
//$arr = $_POST; //若以$.get()方式发送数据,则要改成$_GET.或者干脆:$_REQUEST  
$arr = $_REQUEST; 
$arr['append'] = '测试字符串'; 
//print_r($arr);  
$myjson = my_json_encode($arr); 
echo $myjson; 
 
function my_json_encode($phparr) 

    if(function_exists("json_encode")) 
    { 
      return json_encode($phparr); 
    } 
    else 
    { 
      require_once 'json/json.class.php'; 
      $json = new Services_JSON; 
      return $json->encode($phparr); 
    } 

?> 

摘自 chaojie2009的专栏
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