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;
?>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM