gpt4 book ai didi

php - JQuery 多部分/数据 ajax 发布

转载 作者:可可西里 更新时间:2023-10-31 22:49:24 33 4
gpt4 key购买 nike

我正在使用 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, textStatus, errorThrown) {
//if fails
}
});
return false;
e.preventDefault();
});
}

最佳答案

将其用于 Jquery 多部分/表单数据提交。

$(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);
}
});
}));
});

关于php - JQuery 多部分/数据 ajax 发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37712269/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com