Full Source Code for Form Submit Using AJAX and Jquery with Input type file field

<form class="reg-form" method="post" id="reg-form-data" enctype="multipart/form-data">
<div class="row">
<!--Result-->
<div class="col-12" id="reg_result"></div>
<!--Full Column-->
<div class="col-md-12">
<div class="form-group">
<b>Upload Identity Proof</b> <input class="form-control" id="reg_file" name="regProofdoc" placeholder="Upload ID Proof"
required="" type="file" accept="image/jpeg,image/gif,image/png,application/pdf">
</div>
<div class="form-group">
<textarea class="form-control" id="reg_message"
name="regMessage" placeholder="Detail Description." required></textarea>
</div>
</div>
<!--Button-->
<div class="col-md-12">
<button class="btn btn-large" type="submit"
id="reg_submit_btn"><b>SUBMIT</b>
</button>
</div>
</div>
</form>
<script>
$(document).ready(function(){
$('#reg-form-data').on("submit", function (e) {
e.preventDefault();
var formData = new FormData(this);
formData.append('image', $('input[type=file]')[0].files[0]);
$(':button[type="submit"]').prop('disabled', true);
$(':button[type="submit"]').css({'opacity':'0.5','cursor':'not-allowed'});
$.ajax({
type: 'POST',
url: 'registration.php',
data: formData,
processData: false,
contentType: false,
success: function (response) {
$('#reg_result').html(response);
$('#reg-form-data').trigger('reset');
$("#reg_result").show().delay(5000).fadeOut();
$(':button[type="submit"]').prop('disabled', false);
$(':button[type="submit"]').removeAttr('style');
},
});
});
});
</script>
Note:- Import Jquery.min.js CDN within Head Tag
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
PHP Server Side Program to Save Uploaded files.
<?php
if(isset($_FILES))
{
$regMessage = $_POST['regMessage'];
$currentTimestamp = date("YmdHis");
$targetDirectory = "registration/"; /* Create Directory in root folder of project */
$targetFile = $targetDirectory . $currentTimestamp . '_' . basename($_FILES['image']['name']);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
/* Check if the file already exists */
if (file_exists($targetFile)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
/* Check file size (you can adjust the limit as needed) */
if ($_FILES['image']['size'] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
/* Allow only certain file formats (you can add more if needed) */
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "pdf") {
echo "Sorry, only JPG, JPEG, PNG, and PDF files are allowed.";
$uploadOk = 0;
}
/* Check if $uploadOk is set to 0 by an error */
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
}
else {
if (move_uploaded_file($_FILES['image']['tmp_name'], $targetFile)) {
$imagePath = $targetFile;
echo "Files uploaded successfully.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
