Ajax FormData 使用 POST 类型,但在 PHP 文件中无法获取任何请求


Ajax FormData using POST type but in PHP file no request can be fetched

我无法找出问题所在。如果我缺乏知识并犯了愚蠢的错误,我会提前道歉。这是我的代码段。

    <!-- Piece of html -->
    <div class="form-group">
    <input class="form-control" placeholder="Enter Service image" id="edit_service_image-1" type="file" name="file_image">
    </div>
    <button type="button" class="btn btn-primary" onclick="processIt(<?php echo $service['service_id'];?>,'esi',document.getElementById('edit_service_image-1').files[0])">Edit</button>

javascript函数:->

    function processIt(id,flag,inputvalue)
    {
         var filedata = new FormData();
         filedata.append('imagefile', inputvalue);
         filedata.append('id', id);
         filedata.append('flag', flag);
         $.ajax({
                    url: 'modify.php',
                    type: 'POST',
                    inputvalue: filedata,
                    contentType: false,
                    processData: false,
                    cache: false
                }).success(function(msg) {
                    alert(msg);
                });

    }

这里是修改.php(仅限测试代码)

    <?php
      if(isset($_REQUEST['flag']) && !empty($_REQUEST['flag']) && isset($_REQUEST['id']) && !empty($_REQUEST['id']) )
        {
         $flag = $_REQUEST['flag'];
         $id = $_REQUEST['id'];
         echo "Processed";
        }
       else
        echo "Request not processed";
    ?>

所以我的问题是它总是提醒"请求未处理",这意味着使用 ajax POST 发布任何内容。我很困惑,无法弄清楚确切的问题。

编辑

修改的另一个测试代码.php

     <?php
        print_r($_FILES);
        print_r($_POST);
     ?>

在这种情况下,输出为 -->

    Array{
    }
    Array{
    }

解决

感谢所有指导我的人只是对javascript的更改起作用了。这是processIt()函数中以下更改的代码,它服务于我的目的>

 function processIt(id,flag,inputvalue)
    {
         var filedata = new FormData();
         filedata.append('imagefile', inputvalue);
         filedata.append('id', id);
         filedata.append('flag', flag);
         $.ajax({
                    url: 'modify.php',
                    type: 'POST',
                    data: filedata, // Here instead of inputvalue it's changed to data
                    contentType: false,
                    processData: false,
                    cache: false
                }).success(function(msg) {
                    alert(msg);
                });

    }

我的答案是基于这个答案。不确定您从哪里得到inputvalue,但将其替换为 data.

function processIt(id, flag, inputvalue)
{
     var data = new FormData();
     data.append('imagefile', inputvalue);
     data.append('id', id);
     data.append('flag', flag);
     $.ajax({
        url: 'modify.php',
        type: 'POST',
        data: data,
        contentType: false,
        processData: false,
        cache: false,
        success: function (msg) {
            alert(msg);
        }
     });
}

只需在PHP代码中进行友好的代码审查,避免使用$_REQUEST,并且您在IF条件下进行了太多不必要的检查。这是修复它的一种方法:

if (!empty($_POST['flag']) && !empty($_POST['id'])) {
    $flag = $_POST['flag'];
    $id = $_POST['id'];
    echo "Processed";
} else {
    echo "Request not processed";
}

您也可以尝试filter_input提供一些验证和清理功能的功能。不幸的是,没有INPUT_FILE,您必须使用$_FILES数组。

假设 id 是一个整数,你可以执行以下操作:

$flag = filter_input(INPUT_POST, 'flag', FILTER_SANITIZE_STRING);
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
if ($flag && $id) {
   echo "Processed";
} else {
   echo "Request not processed";
}

您需要将"inputvalue"替换为"data"。

所以你的代码看起来更像这样:

function processIt(id,flag,inputvalue)
{
     var filedata = new FormData();
     filedata.append('imagefile', inputvalue);
     filedata.append('id', id);
     filedata.append('flag', flag);
     $.ajax({
                url: 'modify.php',
                type: 'POST',
                data: { mydata: filedata },
                contentType: false,
                processData: false,
                cache: false
            }).success(function(msg) {
                alert(msg);
            });
}

然后在您的修改中.php执行以下操作:

var_dump($_POST);

查看帖子数据是否可访问。

我不知道

你是怎么得到这个选项的:inputvalue:但这样做可以:

function processIt(id,flag,inputvalue){
    $.ajax({
                    url: 'modify.php',
                    type: 'POST',
                    data: {inputvalue:'imagefile',id:id, flag:flag},
                    contentType: false,
                    processData: false,
                    cache: false,
                    success: function(msg) {
                       alert(msg);
                      }
                });

    }