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

从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
    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();
    }
    
    点赞 评论

相关推荐