php+jquery+ajax+json的一個最簡單實例


html頁面:

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
 $(function(){
  $("#send").click(function(){
   var cont = $("input").serialize();
   $.ajax({
    url:'ab.php',
    type:'post',
    dataType:'json',
    data:cont,
    success:function(data){
     var str = data.username + data.age + data.job;
     $("#result").html(str);
    }
   });
  });  
 });
</script>
</head>
<body>
 <div id="result">一會看顯示結果</div>
 <form id="my" action="" method="post">
  <p><span>姓名:</span><input type="text" name="username" /></p>
  <p><span>年齡:</span><input type="text" name="age" /></p>
  <p><span>工作:</span><input type="text" name="job" /></p>
 </form>
 <button id="send">提交</button>
</body>
</html>




php頁面:

<?php
header("Content-type:text/html;charset=utf-8");
$username = $_POST['username'];
$age = $_POST['age'];
$job = $_POST['job'];
$json_arr = array("username"=>$username,"age"=>$age,"job"=>$job);
$json_obj = json_encode($json_arr);
echo $json_obj;
?>

 


免責聲明!

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



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