drbouzlxb92333332 2019-05-01 22:36
浏览 24
已采纳

从AJAX中检索PHP中formdata的数据

I'm trying to send an image via AJAX to my API via formdata, which then gets passed to a couple of functions to create an image post. The PHP however is throwing out an unidentified index error... and I can't see why.

I generate my formdata with this

var fd = new FormData();
var files = $('#FileInput')[0].files[0];
var user = <?php echo $user_id; ?>;
var bodytext = $("#PostBox").val();
fd.append('file', files);
fd.append('user_id', user);
fd.append('body', bodytext);

Then, send it via AJAX...

$.ajax({
  type: "POST",
  url: "api/createimgpost",
  processData: false,
  contentType: "application/json",
  data: fd,
  success: function(r) {
 console.log(r)
  //location.reload();
        },
  error: function(r) {
   console.log(r)
});

Then retrieve via PHP to pass the data on to my functions...

else if ($_GET['url'] == "createimgpost")
        {
            $user_id = Login::isLoggedIn();
            $poster_id = $_POST['user_id'];

            if ($user_id == $poster_id)
            {           
                $body = isSet($_POST['body']);
                $imgForm = isSet($_FILES['file']);

                $img = Images::Upload($imgForm);

                Post::CreateImgPost($img, $body, $user_id);
            }
        }

In my network tab, it looks like the Form is sent off just fine...

------WebKitFormBoundarybBk6iZXXDNWy6L3K
Content-Disposition: form-data; name="file"; filename="DefaultHeader.png"
Content-Type: image/png


------WebKitFormBoundarybBk6iZXXDNWy6L3K
Content-Disposition: form-data; name="user_id"

1
------WebKitFormBoundarybBk6iZXXDNWy6L3K
Content-Disposition: form-data; name="body"

asdasdasdasd
------WebKitFormBoundarybBk6iZXXDNWy6L3K--

But I'm still getting an unidentified index error on user_id (and I'd assume body and file too). Am I missing something simple? Still kinda a noobie here...

  • 写回答

1条回答 默认 最新

  • dongnaosuan5407 2019-05-01 22:58
    关注

    Update your contentType: to contentType: false,

    Also

    Your checks should look similar to this:

    if( ! isset( $_GET['url'] ) ){ // if $_GET['url'] is not set
        echo 'No URL Set';
        die();
    }
    if ($_GET['url'] == "createimgpost"){
    
        if( ! isset( $_POST['user_id'] ) ){ // if $_POST['user_id'] is not set
            echo 'No User ID Set';
            die();
        }
    
        $user_id = Login::isLoggedIn();
    
        if ( $user_id != $_POST['user_id'] ){
            echo 'No User ID Doesnt Match';
            die();
        }
        if( ! isset( $_POST['body'] ) ){ // if $_POST['body'] is not set
            echo 'No Body Set';
            die();
        }   
        if( ! isset( $_FILES['file'] ) ){ // if $_FILES['file'] is not set
            echo 'File Not Set';
            die();
        }
        if( $_FILES['file']['error'] !==0 ){ // if $_FILES['file'] has errors
            echo $_FILES['file']['error'];
            die();
        }
    
        $img = Images::Upload( $_FILES['file'] );
        Post::CreateImgPost( $img , $_POST['body'] , $user_id );
        //run checks on your POST creation. Whatever it returns for success
    
        echo 'Success';
        die();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)