Image upload functionality is working fine using HTML, Javascript and PHP. But now I am trying to convert it into HTML, JQuery and PHP but I am not able to upload photo using below code.
I am not able to get file type variable on .js page and then file itself on php page (upload_p.php)
Please advise what I am doing wrong. I am trying to get this work for last 2 days but no luck yet.
upload.php
<form name="Uform" id="Uform" novalidate>
<div class="control-group form-group">
<input type="file" name="img" id="img">
<div id="success"></div>
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
upload.js
$(function() {
$("#UForm file").jqBootstrapValidation({
preventSubmit: true,
submitSuccess: function($form, event) {
event.preventDefault();
// Try #1
var form = new FormData() ;
var file = $('#img')[0].files[0];
formData.append("file", file);
// Try #2
var FileData = $('#img').prop('files')[0];
var form_data = new FormData();
form_data.append('file', FileData);
$.ajax({
url: "./user/upload_.php",
type: "POST",
data: {
// Try #1
upload : 1,
data : file
// Try #2
img: img,
},
cache: false,
})
},
});
upload_p.php
$str_photo = "";
if(isset($_FILES['img'])) { $str_photo = trim($_FILES['img']['name']); }
.
.
UploadFile($_FILES["img"]["tmp_name"],$str_img_path);
.
.
SQL Query to insert data into database table
.
.
$response['status']='SUC';
$response['message']="Image uploaded successfully";
echo json_encode($response);
return;