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