PHP+JQUERY操作JSON实例

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP+JQUERY操作JSON的方法。分享给大家供大家参考,具体如下:

json.html 代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>PHP Json传输数据</title>
</head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function(){
$("#submit").click(function(){
var text = $("input").serialize();
$.ajax({
'type':"POST",
'url':'json_encode.php',
'dataType':'json',
'data':text,
success:insertData
});
});
});
function insertData(data){
var str = "姓名="+data.name+"<br/>性别="+data.sex+"<br/>年龄="+data.age;
$("#view").html(str);
}
</script>
<body>
姓名:<input name="name" id="name" type="text" value=""><br/>
性别:<input name="sex" id="sex" type="text" value=""><br/>
年龄:<input name="age" id="age" type="text" value="">
<input type="submit" name="submit" id="submit" value="提交">
<div style="font-size:14px;" id="view">
</div>
</body>
</html>

json_encode.php 代码

<?php
header("Content-type:text/html;charset=utf8");
include("Json.php");
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$json_arg = array('name'=>$name,'sex'=>$sex,'age'=>$age);
$json = new JSON;
$json_result = $json->encode($json_arg);
echo $json_result;
?>

PS:这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在线格式化工具:
http://tools.jb51.net/code/jsonformat

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat

C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP中json格式数据操作技巧汇总》、《PHP数学运算技巧总结》、《PHP基本语法入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

PHP函数超时处理方法

本文实例讲述了PHP函数超时处理方法。分享给大家供大家参考,具体如下: register_shutdown_function Registers the function named b...

PHP实现将base64编码字符串转换成图片示例

PHP实现将base64编码字符串转换成图片示例

本文实例讲述了PHP实现将base64编码字符串转换成图片。分享给大家供大家参考,具体如下: 步骤: 1. 获取base64文件: 复制代码 代码如下:$image="data:imag...

PHP中cookie知识点学习

什么是cookie cookie,即小饼干,是保存在用户代理端(浏览器是最常见的用户代理)的一些数据片段。浏览网页时,浏览器会将 当前页面有效的 cookie放在请求的头部发送到服务端。...

ueditor 1.2.6 使用方法说明

ueditor 1.2.6 使用方法说明

本文以php版本为例:文件下载:http://ueditor.baidu.com/website/download.html还可以自己先定义内容,然后下载,这样可以帮助我们精简不少东西。...

php正则表达式获取内容所有链接

方法一: function get_all_url($code){ preg_match_all('/<as+href=["|']?([^>"' ]+)...