jquery.form.js 提交教会你怎样用ajaxForm

<html>
<head>
<meta content="text/html" charset=utf-8 />
<script src="jquery-1.7.1.min.js"></script>
<script src="jquery.form.js"></script>
<script type="text/javascript">
       // wait for the DOM to be loaded


          $(document).ready(function() {
var optionss = { 
dataType:"text", 
 type:'post', 
 url: "chuli1.php", 
 beforeSubmit:showRequest,  
  success:showResponse,
  clearForm:true ,
}; 


          $('#myForm').ajaxForm(optionss);
      function showRequest() {
          if($("#name").val()==''){
   alert("name不能为空");
exit;
 }
       }
 function showResponse(data){
    var $datas=eval("("+data+")");
alert($datas.name+"*****"+$datas.comment);
 }


         });


     </script>


</head>
<body>
  <form id="myForm" action="chuli1.php" method="post">


     Name: <input type="text" name="name" id="name" />


     Comment: <textarea name="comment"></textarea>


     <input type="submit" value="Submit Comment" />


</form>


</body>

</html>


chuli1.php

<?php
header("Content:text/html; charset=utf8");
if($_POST['name']=='admin'){
  $array=array("name"=>$_REQUEST["name"],"comment"=>$_REQUEST['comment']);
  echo json_encode($array);
}else{$array=array("name"=>"MSD:","comment"=>"error");
echo json_encode($array);
}
?>

你可能感兴趣的:(jquery.form.js)