php – JQuery multipart / data ajax post

前端之家收集整理的这篇文章主要介绍了php – JQuery multipart / data ajax post前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 JQuery发布表单数据,我已经将其添加到我的函数中,以允许它发布/上传文件
  1. mimeType:"multipart/form-data",

我在这里以HTML格式呼叫:

  1. <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处理附件:

  1. $attachment_array = array();
  2. foreach($_FILES['ticket_update_files']['name'] as $key => $value) {
  3. if(!$_FILES['ticket_update_files']['error'][$key]) {
  4.  
  5. }
  6. }

但是它不能识别任何已被选择的文件.

我的完整jquery功能是:

  1. function post_form(form_id,type,redir_url,loading_modal) {
  2. type = type || '';
  3. redir_url = redir_url || '';
  4. loading_modal = loading_modal || '';
  5.  
  6. $( form_id ).submit(function(e) {
  7. var formObj = $(this);
  8. var formURL = formObj.attr("action");
  9. var formData = new FormData(this);
  10.  
  11. Checkrequired(e);
  12.  
  13. if(loading_modal === '1') { } else {
  14. LoadModalBody('<h2 align="center">Loading...</h3><p align="center"><i class="fa fa-refresh fa-spin fa-5x"></i></p>','Loading');
  15. }
  16.  
  17. $.ajax({
  18. url : '/section' + formURL,type: "POST",data : formData,mimeType:"multipart/form-data",contentType: false,cache: false,processData:false,success:function(data,textStatus,jqXHR) {
  19. //alert(type);
  20. if(type === 'modal') {
  21. if(redir_url === '') {
  22. LoadModal('/section' + formURL,'');
  23. } else {
  24. LoadModal('/section' + redir_url,'');
  25. }
  26. } else if(type === 'reload') {
  27. if(redir_url === '') {
  28. location.reload();
  29. } else {
  30. OpenPage(redir_url);
  31. }
  32. } else {
  33. //close the loading modal
  34. if(loading_modal === '1') { } else {
  35. CloseModal();
  36. }
  37. //location.reload();
  38. //$("body").html(data);
  39. }
  40. },error: function(jqXHR,errorThrown) {
  41. //if fails
  42. }
  43. });
  44. return false;
  45. e.preventDefault();
  46. });
  47. }
用于Jquery multipart / form-data提交.
  1. $(document).ready(function (e) {
  2. $("#formid").on('submit',(function (e) {
  3. e.preventDefault();
  4. $("#message").empty();
  5. $('#loading').show();
  6. $.ajax({
  7. url: "ajax_PHP_villa_file.PHP",// Url to which the request is send
  8. type: "POST",// Type of request to be send,called as method
  9. data: new FormData(this),// Data sent to server,a set of key/value pairs (i.e. form fields and values)
  10. contentType: false,// The content type used when sending data to the server.
  11. cache: false,// To unable request pages to be cached
  12. processData: false,beforeSend: function () {
  13. $('.loader-img').show();
  14. },// To send DOMDocument or non processed data file it is set to false
  15. success: function (data) // A function to be called if request succeeds
  16. {
  17. $('.loader-img').hide();
  18. if (data.trim() != "")
  19. $("#imresss").html(data);
  20. }
  21. });
  22. }));
  23. });

猜你在找的PHP相关文章