简体   繁体   English

jQuery POST 数据使用 FormData 和 PHP

[英]jQuery POST data using FormData with PHP

I'm trying to post data using ajax, jquery, formdata and then process it using php i'm not able to see where is the error to process it in my PHP file. I'm trying to post data using ajax, jquery, formdata and then process it using php i'm not able to see where is the error to process it in my PHP file. The final result is an empty array.最终结果是一个空数组。

HTML CODE HTML 代码

<form action="ajax.php?action=upload" method="POST" name="submit_upload_album" id="submit_upload_album" enctype="multipart/form-data">
    <input type="hidden" id="input1" name="input1" value="input1">
    <input type="hidden" id="input2" name="input2" value="input2">
    <input type="hidden" id="input3" name="input3" value="input3">
    <input type="hidden" id="input4" name="input4" value="false">
    <input type="hidden" id="input5" name="input5">
    <input type="hidden" id="input6" name="input6" value="auto">
    <input id="title" name="title" class="form-control" type="text" maxlength="70" required/>
    <textarea id="description" class="form-control" name="description" rows="4" style="resize: vertical;"></textarea>
    <input id="file1" type="file" name="fileupload[]" accept="video/*" required/>
    <input id="file2" type="file" name="fileupload[]" accept="audio/*" required/>
    <input id="file3" type="file" name="fileupload[]" accept="video/*" required/>
</form>

JS code JS代码

<script type="text/javascript">
    $(document).ready(function(e){
        // Submit form data via Ajax
        $("#submit_upload_album").on('submit', function(e){
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: "ajax.php?action=upload",
                data: new FormData(this),
                async: true,
                beforeSend: function(){
                    /*$('input[type=submit]').attr("disabled","disabled");*/
                },
                success: function(response){
                    $('#answer').html(response);
                },
                contentType: false,
                cache: false,
                processData:false
            });
        });
    });
</script>

PHP Code PHP 代码

<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST');
    header("Access-Control-Allow-Headers: X-Requested-With");
    echo '<pre>';
    var_dump($_FILES);
    echo '<br>';
    var_dump($_POST);
    echo '</pre>';
?>

Result结果

array(0) {}

array(0) {}

$(this) is not accessible in ajax request you should modify your code as follows: $(this) 在 ajax 请求中不可访问,您应该按如下方式修改代码:

$("#submit_upload_album").on('submit', function(e){
            e.preventDefault();
            let formData = $(this).serialize();
            $.ajax({
                type: 'POST',
                url: "ajax.php?action=upload",
                data: new FormData(formData),
                async: true,
                beforeSend: function(){
                    /*$('input[type=submit]').attr("disabled","disabled");*/
                },
                success: function(response){
                    $('#answer').html(response);
                },
                contentType: false,
                cache: false,
                processData:false
            });
        });

Hopefully this is helpful for you希望这对你有帮助

After checking the PHP configuration the problem was coming from检查 PHP 配置后,问题来自

post_max_size and upload_max_filesize post_max_size 和 upload_max_filesize

they were limited on 2M so it was blocking all other posted parameters.它们被限制在 2M 上,因此它阻止了所有其他发布的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript和XMLHTTPRequest将FormData发布到php - POST FormData to php using javascript and XMLHTTPRequest 通过jQuery和FormData的Ajax POST请求 - $ _POST在PHP上为空 - Ajax POST request via jQuery and FormData - $_POST empty on PHP 使用formdata通过jQuery Ajax将带有文件输入,文本输入和多个复选框的表单发布到PHP - Post form with fileinput, textinput and multiple checkboxes to php via jquery ajax using formdata 使用jQuery AJAX使用二进制数据发送FormData - Sending FormData with a binary data by using jQuery AJAX 使用formdata使用ajax将数据发送到PHP - Send data to PHP with ajax using formdata 使用jQuery Ajax和FormData跟踪ajax发布文件上传的进度 - Track ajax post progress for fileupload using jquery ajax and FormData 使用POST类型的Ajax FormData,但在PHP文件中无法获取任何请求 - Ajax FormData using POST type but in PHP file no request can be fetched 使用 formData 提交表单后,php 中的 $_POST 数组为空 - $_POST array empty in php after submission of a form using a formData 使用 Javascript FormData()、AJAX $.post 和 PHP 进行简单文件上传 - Simple file upload using Javascript FormData(), AJAX $.post and PHP 使用 jQuery val() 发送表单数据并使用 FormData 发送数据 - Using jQuery val() to send form data and using FormData to send for data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM