I can retrieve input type text, textarea, select on Ajax / JQuery page. Then variable values are passed to PHP process page where data are retrieve using POST method and data inserted into database table. All things are working fine.
But when I try to retrieve value of input type file variable on Ajax / Query page, it is giving blank value. I tried different codes to do it which I found from internet.
Please advise so I can make necessary changes in my script to make it working.
personal_details.php
<form name="AddForm" id="AddForm" novalidate>
<div class="control-group form-group">
.
.
<input type="file" name="file_photo" id="file_photo">
.
.
other fields like Name, Mail etc
.
.
<div id="success"></div>
<!-- For success/fail messages -->
<button type="submit" class="btn btn-primary">Send Message</button>
</div>
</form>
personal_details.js
$(function() {
$("#AddForm input,#AddForm textarea, #AddForm file").jqBootstrapValidation({
preventSubmit: true,
submitSuccess: function($form, event) {
event.preventDefault();
var name = $("input#name").val();
var email = $("input#email").val();
.
.
var file_photo = $("file#file_photo").val();
//var file_photo = $('#file_photo')[0].files[0];
//var file_photo = document.getElementById("file_photo").files[0];
$.ajax({
url: "./user/personal_details_p.php",
type: "POST",
data: {
name: name,
email: email,
file_photo: file_photo,
},
cache: false,
success: function(data)
{
//alert(data);
var $ResponseText_L=JSON.parse(data);
.
.
if condition
.
.
},
})
},
});
personal_details_p.php
$str_name = "";
if (isset($_POST["name"])) { $str_name = trim($_POST["name"]); }
$str_email = "";
if (isset($_POST["email"])) { $str_email = trim($_POST["email"]); }
$str_photo = "";
if(isset($_FILES['file_photo'])) { $str_photo = trim($_FILES['file_photo']['name']); }
.
.
SQL Query to insert data
.
.
$response['status']='SUC';
$response['message']="Data inserted successfully";
echo json_encode($response);
return;