PHP代码
- <?php
- header("content-type:text/html;charset=utf-8");
- $username = $_POST['hiusername'];
- $sex = $_POST['hisex'];
- $age = $_POST['hiage'];
- //$json_res = {'username':$username,'sex':$sex,'age':$age};
- $json_res = array("username"=>$username,'sex'=>$sex,'age'=>$age);
- $res = json_encode($json_res);
- file_put_contents("c:/1.txt",$res);
- echo $res;
- ?>
XML/HTML代码
- <html>
- <head>
- <title>php jquery json数据传输</title>
- <meta http-equiv="content-type" content="text/html;charset=utf-8" />
- <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
- <script type="text/javascript">
- $(function(){
- $('#send').click(function(){
- $.ajax({
- type: "post",
- url: "/php/s1.php",
- data: {hiusername:$("input[name='username']").val(),hisex:$("input[name='sex'][checked]").val(),hiage:$("input[name='age']").val()},
- dataType: "json",
- success: function(msg){
- if(msg!=null){
- alert("提交成功!")
- }
- },error:function(){
- alert("ddddd")
- }
- });
- });
- });
- </script>
- </head>
- <body>
- <!-- <form action="/php/s1.php" method="post"> -->
- 姓名:<input type="text" name="username" /><br />
- 性别:<input type="radio" name="sex" value="男" />男<input type="radio" name="sex" value="女" />女<br />
- 年龄:<input type="text" name="age" />
- <input type="submit" name="sub" id="send" value="提交" />
- <!-- </form> -->
- <div id="test"></div>
- </body>
- </html