我正在使用
JQuery发布表单数据,我已经将其添加到我的函数中,以允许它发布/上传文件:
mimeType:"multipart/form-data",
我在这里以HTML格式呼叫:
<form id="form1" method="post" action="/tickets/record?type=<?PHP echo $_GET["type"]; ?>&seq=<?PHP echo $_GET["seq"]; ?>" enctype="multipart/form-data" onsubmit="post_form('#form1');">
并尝试用PHP处理附件:
$attachment_array = array(); foreach($_FILES['ticket_update_files']['name'] as $key => $value) { if(!$_FILES['ticket_update_files']['error'][$key]) { } }
但是它不能识别任何已被选择的文件.
我的完整jquery功能是:
function post_form(form_id,type,redir_url,loading_modal) { type = type || ''; redir_url = redir_url || ''; loading_modal = loading_modal || ''; $( form_id ).submit(function(e) { var formObj = $(this); var formURL = formObj.attr("action"); var formData = new FormData(this); Checkrequired(e); if(loading_modal === '1') { } else { LoadModalBody('<h2 align="center">Loading...</h3><p align="center"><i class="fa fa-refresh fa-spin fa-5x"></i></p>','Loading'); } $.ajax({ url : '/section' + formURL,type: "POST",data : formData,mimeType:"multipart/form-data",contentType: false,cache: false,processData:false,success:function(data,textStatus,jqXHR) { //alert(type); if(type === 'modal') { if(redir_url === '') { LoadModal('/section' + formURL,''); } else { LoadModal('/section' + redir_url,''); } } else if(type === 'reload') { if(redir_url === '') { location.reload(); } else { OpenPage(redir_url); } } else { //close the loading modal if(loading_modal === '1') { } else { CloseModal(); } //location.reload(); //$("body").html(data); } },error: function(jqXHR,errorThrown) { //if fails } }); return false; e.preventDefault(); }); }
用于Jquery multipart / form-data提交.
$(document).ready(function (e) { $("#formid").on('submit',(function (e) { e.preventDefault(); $("#message").empty(); $('#loading').show(); $.ajax({ url: "ajax_PHP_villa_file.PHP",// Url to which the request is send type: "POST",// Type of request to be send,called as method data: new FormData(this),// Data sent to server,a set of key/value pairs (i.e. form fields and values) contentType: false,// The content type used when sending data to the server. cache: false,// To unable request pages to be cached processData: false,beforeSend: function () { $('.loader-img').show(); },// To send DOMDocument or non processed data file it is set to false success: function (data) // A function to be called if request succeeds { $('.loader-img').hide(); if (data.trim() != "") $("#imresss").html(data); } }); })); });