php – JQuery multipart / data ajax post

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – JQuery multipart / data ajax post脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 JQuery发布表单数据,我已经将其添加到我的函数中,以允许它发布/上传文件
mimeTyPE:"multipart/form-data",

在这里html格式呼叫:

<form id="form1" method="post" action="/tickets/record?type=<?PHP echo $_GET["type"]; ?>&amp;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);
            }
        });
    }));
});

脚本宝典总结

以上是脚本宝典为你收集整理的php – JQuery multipart / data ajax post全部内容,希望文章能够帮你解决php – JQuery multipart / data ajax post所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。