简体   繁体   English

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

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

I am unable to figure out the problem .I apology in advance if I have lack of knowledge and did silly mistakes. 我无法解决问题。如果我缺乏知识并且犯了愚蠢的错误,我会提前道歉。 Here is my piece of code. 这是我的代码。

    <!-- 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>

The javascript function :-> 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);
                });


    }

Here is modify.php (Only test code) 这是modify.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";
    ?>

So my problem is its always alerting "Request Not Processed", which means nothing is getting posted using ajax POST . 所以我的问题是它总是警告“请求未处理”,这意味着使用ajax POST不会发布任何内容。 I am confused, couldn't figure out the exact problem. 我很困惑,无法找出确切的问题。

Edit 编辑

Another test code of modify.php 另一个modify.php的测试代码

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

In this case the output is --> 在这种情况下,输出为->

    Array{

    }
    Array{

    }

SOLVED 解决了

Thanks to everybody who guided me Just a change in javascript worked . 感谢指导我的每个人。只是对javascript进行了修改。 Here's the following changed code in processIt() function, which served my purpose-> 这是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);
                });


    }

I am basing my answer off this answer . 我的答案以这个答案为基础 Not sure where you got inputvalue from, but replace it with data . 不确定从哪里获得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);
        }
     });
}

And just a friendly code review in your PHP code, avoid using $_REQUEST and you are doing too much unnecessary checking in your IF condition. 而且,只需在PHP代码中进行友好的代码审查即可,避免使用$_REQUEST并且在IF条件下进行了不必要的检查。 Here's one way to fix it: 这是修复它的一种方法:

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

You may also try filter_input function which offers some validation and sanitization features. 您也可以尝试filter_input函数,该函数提供了一些验证和清除功能。 Unfortunately, there is no INPUT_FILE and you have to use $_FILES array. 不幸的是,没有INPUT_FILE,您必须使用$_FILES数组。

Assuming id is an integer, you could do something like: 假设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";
}

I have no idea how did you get this option: inputvalue: but this will do: 我不知道你是怎么得到这个选项的: 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);

                      }

                });


    }

You need to replace your "inputvalue" with "data". 您需要将“输入值”替换为“数据”。

So your code will look more like this: 因此,您的代码将更像这样:

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

And then in your modify.php do: 然后在您的Modify.php中执行以下操作:

var_dump($_POST);

To see if the post data is accessable. 查看帖子数据是否可访问。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM