I have these 2 files index.php and uploadcover.inc.php and submitting the form of index.php with the input field as file, it lets us upload an image to set as the cover picture. The code works fine as it is, but I want to modify this index.php file to use AJAX for implementing this without the page being refreshed and the redirected from uploadcover.inc.php back to index.php using the header function.Please help me out.
index.php =>
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<form action="include/uploadcover.inc.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="cover-upload" style="display:none" onchange="this.form.submit();">
<label for="cover-upload" class="fa fa-camera fa-2x" aria-hidden="true"></label>
</form>
</body>
</html>
uploadcover.inc.php =>
<?php
session_start();
include_once 'dbh.inc.php';
$sessionid = $_SESSION['u_id'];
$file= $_FILES['file'];
$fileName= $file['name'];
$fileTmpName= $file['tmp_name'];
$fileSize= $file['size'];
$fileError= $file['error'];
$fileType= $file['type'];
$fileExt = explode('.',$fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg','jpeg','png','gif');
if(in_array($fileActualExt,$allowed)){
if($fileError=== 0){
if($fileSize<3145728){
$fileNameNew = "cover".$sessionid.".".$fileActualExt;
$fileDestination = '../profile/'.$fileNameNew;
move_uploaded_file($fileTmpName,$fileDestination);
$sql = "UPDATE coverimg SET status=0 WHERE user_id='$sessionid'";
mysqli_query($conn,$sql);
header("Location: ../index.php?upload=success");
} else {
$_SESSION['errors']['size'] = 1;
session_write_close();
header("Location: ../index.php?upload=size_exceeded_3MB");
}
} else {
$_SESSION['errors']['upload'] = 1;
session_write_close();
header("Location: ../index.php?upload=error");
}
} else{
$_SESSION['errors']['type'] = 1;
session_write_close();
header("Location: ../index.php?upload=typeerror");
}
Help, me out with the AJAX code for the index.php file to implement the same without the need for headers in uploadcover.inc.php.