<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery.min.js"></script> <script src="jquery.form.js"></script> </head> <body> <form id="myForm" action="do.action" method="post"> 姓名: <input type="text" name="name" /> </br> 性别: <input type="radio" name="sex" value="1">男<input type="radio" name="sex" value="0">女</br> 邮箱:<input type="text" name="email"></br></br> <input type="file" name="files" id="test"> <input type="submit" value="提交" /> </form> <script type="text/javascript"> $('#myForm').on("submit",function() { //懒人建站整理 $(this).ajaxSubmit({ url: 'do.php', //默认是form的action type: 'post', //默认是form的method(get or post) dataType: "json", //html(默认), xml, script, json...接受服务端返回的类型 clearForm: true, //成功提交后,清除所有表单元素的值 resetForm: true, //成功提交后,重置所有表单元素的值 //target: '#output', //把服务器返回的内容放入id为output的元素中 //timeout: 3000, //限制请求的时间,当请求大于3秒后,跳出请求 //提交前的回调函数 beforeSubmit: function(arr,$form,options){ //formData: 数组对象,提交表单时,Form插件会以Ajax方式自动提交这些数据,格式如:[{name:user,value:val },{name:pwd,value:pwd}] //jqForm: jQuery对象,封装了表单的元素 //options: options对象 //比如可以再表单提交前进行表单验证 console.log("beforeSubmit",arr,$form,options) }, //提交成功后的回调函数 success: function(data,status,xhr,$form){ console.log("success",data,status,xhr,$form); //alert(data.notice_content); if(data.Flag){ //console.log(data.Content) } }, error: function(xhr, status, error, $form){ //console.log("error",xhr, status, error, $form) }, complete: function(xhr, status, $form){ //console.log("complete",xhr, status, $form) } } ); return false; //阻止表单默认提交 }); </script> </body> </html>
<?php /** * Created by PhpStorm. * User: hanks * Date: 2017/5/18 * Time: 15:30 */ $arr=[]; if(isset($_FILES['files'])){ $data_list=$_POST; $data=$_FILES['files'];var_dump($data);var_dump($data_list); }else{ $arr=['status'=>0,'notice_content'=>'没有数据!']; exit(json_encode($arr,true)); } exit(json_encode($data,true)); /** * array (size=5) 'name' => string '123456.png' (length=10) 'type' => string 'image/png' (length=9) 'tmp_name' => string '/tmp/phpVOxtir' (length=14) 'error' => int 0 'size' => int 50140 /mnt/hgfs/www/test/do.php:11: array (size=3) 'name' => string 'hanks' (length=5) 'sex' => string '1' (length=1) 'email' => string 'hanks135******24@gmail.com' (length=26) */