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 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题