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" >
</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; ?>
|
请发表评论