I want to upload PDF file through Ajax in php but I could not complete the code. Please help
My Form is looks like this
<form action="javascript:void(0)" method="post" id="ajax-form" enctype="multipart/form-data>
<input type="" name="user_id" value="<?= $_SESSION['id']?>" hidden ><!---user id from tb_user table-->
<input type="" name="user_name" value="<?= $_SESSION['reader_name']?>" hidden > <!--user name from tb_user--->
<input type="" name="aid" value="<?= $aid?>" hidden > <!---assignment id from tb_assignment----->
<input type="" name="aname" value="<?= $q['assignment']?>" hidden> <!------assignment name from tb_assignment-------->
<input type="" name="a_answerid" value="<?= $row['id']?>" hidden><!---assignment answer ID from assignment answer table--->
<textarea class="form-control" id="textAreaAdjust" name="reply" style="overflow:auto" placeholder="Write your comment here"></textarea>
<label for="formFile" class="form-label">Default file input example</label>
<input class="form-control" type="file" id="formFile">
<div class="col-lg-3 col-sm-3">
<input type="submit" class="btn btn-outline-dark mt-2 text-left mb-4" name="submit" value="Reply">
</div>
</form>
And my Ajax code is looks like this. I am confused how to write code for ajax to submit the PDF file.
<script type="text/javascript">
$(document).ready(function($){
// hide messages
$("#error").hide();
$("#show_message").hide();
// on submit...
$('#ajax-form').submit(function(e){
e.preventDefault();
$("#error").hide();
//First name required
var reply = $("input#reply").val();
if(reply == ""){
$("#error").fadeIn().text("Please Write Something");
$("input#reply").focus();
return false;
}
var userid = $("input#user_id").val();
if(userid == ""){
$("#error").fadeIn().text("Please Write Something");
$("input#user_id").focus();
return false;
}
var uname = $("input#user_name").val();
if(uname == ""){
$("#error").fadeIn().text("Please Write Something");
$("input#user_name").focus();
return false;
}
var aid = $("input#aid").val();
if(aid == ""){
$("#error").fadeIn().text("Please Write Something");
$("input#aid").focus();
return false;
}
var aname = $("input#aname").val();
if(aname == ""){
$("#error").fadeIn().text("Please Write Something");
$("input#aname").focus();
return false;
}
var aname = $("input#a_answerid").val();
if(a_answerid == ""){
$("#error").fadeIn().text("Please Write Something");
$("input#a_answerid").focus();
return false;
}
// ajax
$.ajax({
type:"POST",
url: "comment.php",
data: $(this).serialize(), // get all form field value in serialize form
success: function(){
location.reload();
}
});
});
return false;
});
</script>
And my php code for action is as below
extract($_POST);
if(mysqli_query($con, "INSERT INTO tb_comment(comment, user_id, user_name, a_id, a_name, a_answerid) VALUES('" . $reply . "', '" . $user_id . "', '" . $user_name . "', '" . $aid . "', '" . $aname . "' , '" . $a_answerid . "')")) {
echo '1';
exit;
} else {
echo "Error: " . $sql . "" . mysqli_error($con);
}
mysqli_close($con);
Please help me to submit a file through this ajax.