index.php
<html> <head> <title>jQuery Test</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#submit").click(function(){ $.ajax({ url: "text.php", type: "POST", data: { id: $("#id").val(), firstName: $("#firstName").val(), lastName: $("#lastName").val(), email: $("#email").val() }, dataType: "JSON", success: function (jsonStr) { var jwu = JSON.parse(JSON.stringify(jsonStr));//解析 if (jwu.json_sys =='1'){ $("#result").text(JSON.stringify(jsonStr)); }else{ alert(jwu.json_txt); } } }) .error(function() { alert("發生錯誤!"); }) //.complete(function() { alert("complete"); }); }); }); </script> </head> <body> <div id="result"></div> <form name="contact" id="contact" method="post"> Id : <input type="text" name="id" id="id"/><br/> firstName : <input type="text" name="firstName" id="firstName"/><br/> lastName : <input type="text" name="lastName" id="lastName"/><br/> email : <input type="text" name="email" id="email"/><br/> <input type="button" value="Get It!" name="submit" id="submit"/> </form> </body> </html>
text.php
<?php header('Content-type: application/json'); $id =$_POST["id"]; $firstName=$_POST["firstName"]; $lastName =$_POST["lastName"]; $email =$_POST["email"]; if($id<0){ $data=array( "json_sys"=> '0',//1表示回傳成功0表示作業失敗 "json_txt"=> '發生錯誤,無法存檔' ); }else{ $data=array( "json_sys"=> '1',//1表示回傳成功0表示作業失敗 ); } echo json_encode($data); ?>
文章標籤
全站熱搜
留言列表