PHP代码
  1. <?php  
  2. header("content-type:text/html;charset=utf-8");  
  3. $username = $_POST['hiusername'];  
  4. $sex = $_POST['hisex'];  
  5. $age = $_POST['hiage'];  
  6. //$json_res = {'username':$username,'sex':$sex,'age':$age};  
  7. $json_res = array("username"=>$username,'sex'=>$sex,'age'=>$age);  
  8. $res = json_encode($json_res);  
  9. file_put_contents("c:/1.txt",$res);  
  10. echo $res;  
  11.   
  12. ?>  
XML/HTML代码
  1. <html>  
  2. <head>  
  3. <title>php jquery json数据传输</title>  
  4. <meta http-equiv="content-type" content="text/html;charset=utf-8" />  
  5. <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>  
  6. <script type="text/javascript">  
  7. $(function(){  
  8.     $('#send').click(function(){  
  9.          $.ajax({  
  10.              type: "post",  
  11.              url: "/php/s1.php",  
  12.              data: {hiusername:$("input[name='username']").val(),hisex:$("input[name='sex'][checked]").val(),hiage:$("input[name='age']").val()},  
  13.              dataType: "json",  
  14.              success: function(msg){  
  15.                   if(msg!=null){  
  16.                          alert("提交成功!")  
  17.                   }  
  18.              },error:function(){  
  19.                          alert("ddddd")  
  20.              }  
  21.          });  
  22.     });  
  23. });  
  24.   
  25. </script>  
  26. </head>  
  27. <body>  
  28. <!-- <form action="/php/s1.php" method="post">  -->  
  29. 姓名:<input type="text" name="username" /><br />  
  30. 性别:<input type="radio" name="sex" value="男" /><input type="radio" name="sex" value="女" /><br />  
  31. 年龄:<input type="text" name="age" />  
  32. <input type="submit" name="sub" id="send" value="提交" />  
  33. <!-- </form> -->  
  34.   
  35. <div id="test"></div>  
  36. </body>  
  37. </html  

 

H5/JS/CSS | 评论(0) | 引用(0) | 阅读(4653)