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 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件
  • ¥15 一个主机电脑有两个显示器,当前有两个软件主体是网页html打包的exe程序,如何通过cmd命令bat同时打开软件一个在主屏幕显示,另外一个软件在2副屏幕上显示