通过Ajax将文件数据、文本数据和变量数据发布到PHP文件以供上传

通过Ajax将文件数据、文本数据和变量数据发布到PHP文件以供上传,php,jquery,ajax,Php,Jquery,Ajax,我有三种类型的输入,即文件输入、文本输入和变量输入。我想通过使用Ajax JSON将数据发送到PHP文件来上传这些输入。我还想知道如何在PHP文件中捕获这些数据 我使用的HTML代码没有表单语法。变量数据名,在JQuery代码中为val1 HTML代码: <div class="container" id="post"> <textarea id="posttext" autocomplete="off"></textarea> <inp

我有三种类型的输入,即文件输入、文本输入和变量输入。我想通过使用Ajax JSON将数据发送到PHP文件来上传这些输入。我还想知道如何在PHP文件中捕获这些数据

我使用的HTML代码没有表单语法。变量数据名,在JQuery代码中为val1

HTML代码:

<div class="container" id="post">
    <textarea id="posttext" autocomplete="off"></textarea>
    <input type="file" name="file" id="file" multiple accept=".mp4, .mov, .m4v, .MPEG-4, .gif, .jpg, .png"/>
    <button type="button" id="submitpost">Submit</button>
</div>
PHP代码:

<?php
if (!isset($_POST['VALUES']) && !empty($_POST['VALUES'])) {
        $params = $_POST['VALUES'];

}
?>

如何在PHP中获取每个值以上载文件并将文本和变量数据插入数据库

使用以下代码:

Html:

<div class="container" id="post">
  <form enctype="multipart/form-data" method="POST" id="myform">
    <textarea id="posttext" name="posttext" autocomplete="off"></textarea>
    <input type="file" name="file" id="file" multiple accept=".mp4, .mov, .m4v, .MPEG-4, .gif, .jpg, .png"/>
    <button type="submit" name="submitpost" id="submitpost">Submit</button>
   </form>
</div>
$(document).on("click", "#submitpost", function(e){
    $("form#myform").submit();
});
$("form#myform").on('submit', function(e){
    e.preventDefault();
    var val1 = "Some Data";
    var file = this.files[0];
    var form = new FormData();
    form.append('file', file);
    form.append('val1', val1);
    form.append('posttext', $('#posttext').val());
    $.ajax({
        url : "post.php",
        type: "POST",
        cache: false,
        async: false,
        contentType: false,
        processData: false,
        data : form,
        success: function(response){
            alert(response);
        }
    });
});
<?php
if (isset($_POST) && !empty($_POST)) {
    print_r($_POST);
    print_r($_FILES);
}
?>
PHP代码:

<div class="container" id="post">
  <form enctype="multipart/form-data" method="POST" id="myform">
    <textarea id="posttext" name="posttext" autocomplete="off"></textarea>
    <input type="file" name="file" id="file" multiple accept=".mp4, .mov, .m4v, .MPEG-4, .gif, .jpg, .png"/>
    <button type="submit" name="submitpost" id="submitpost">Submit</button>
   </form>
</div>
$(document).on("click", "#submitpost", function(e){
    $("form#myform").submit();
});
$("form#myform").on('submit', function(e){
    e.preventDefault();
    var val1 = "Some Data";
    var file = this.files[0];
    var form = new FormData();
    form.append('file', file);
    form.append('val1', val1);
    form.append('posttext', $('#posttext').val());
    $.ajax({
        url : "post.php",
        type: "POST",
        cache: false,
        async: false,
        contentType: false,
        processData: false,
        data : form,
        success: function(response){
            alert(response);
        }
    });
});
<?php
if (isset($_POST) && !empty($_POST)) {
    print_r($_POST);
    print_r($_FILES);
}
?>


变量数据(val1)在哪里\ajax正在工作我将每个“文本”代码放在php文件中,我得到文本响应,但没有得到值。我得到的响应如下数组([file]=>undefined[val1]=>一些数据[posttext]=>jjj)数组(),但文件未定义使用var file=this.files;这样地。我得到了var file=this.files[0]的错误;