drt41563 2016-02-05 12:28
浏览 190
已采纳

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

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

    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)

    <?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 . I am confused, couldn't figure out the exact problem.

Edit

Another test code of 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 . Here's the following changed code in processIt() function, which served my purpose->

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


    }
  • 写回答

3条回答 默认 最新

  • dpd7195 2016-02-05 13:27
    关注

    I am basing my answer off this answer. Not sure where you got inputvalue from, but replace it with 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. 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. Unfortunately, there is no INPUT_FILE and you have to use $_FILES array.

    Assuming id is an integer, you could do something like:

    $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";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥15 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 stm32标准库的PID角度环
  • ¥15 ADS已经下载好了,但是DAS下载不了,一直显示这两种情况,有什么办法吗,非常急!