At the moment I am using AJAX on a form where the user uploads their photo, it goes to the php to check the file and will retu a message depending if it is successful or not. I am echoing the result into a paragraph tag so at the moment my PHP will header the user to the page I want but it loads the page in the tabbed content where I am echoing the result.
I caot really use the success of the AJAX to do it because it is a success even if an error message is displayed to the user
Here is my AJAX
$("#avatar_form").submit(function (event) {
//disable the default form submission
//grab all form data
var formData = new FormData($(this)[0]);
$.ajax({
url: 'photo_system.php',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (result) {
console.log(result);
$('#status').html(result);
}
});
event.preventDefault();
});
PHP
$result = "";
if (isset($_FILES["avatar"]["name"]) && $_FILES["avatar"]["tmp_name"] != "") {
$fileName = $_FILES["avatar"]["name"];
$fileTmpLoc = $_FILES["avatar"]["tmp_name"];
$fileType = $_FILES["avatar"]["type"];
$fileSize = $_FILES["avatar"]["size"];
$fileErrorMsg = $_FILES["avatar"]["error"];
$kaboom = explode(".", $fileName);
$fileExt = end($kaboom);
list($width, $height) = getimagesize($fileTmpLoc);
if ($width < 10 || $height < 10) {
$result = "That image has no dimensions";
echo $result;
exit();
}
$db_file_name = rand(100000000000, 999999999999) . "." . $fileExt;
if ($fileSize > 1048576) {
$result = "Your image file was larger than 1mb";
echo $result;
exit();
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName)) {
$result = "Please only JPG, GIF or PNG images";
echo $result;
exit();
} else if ($fileErrorMsg == 1) {
$result = "An unknown error occurred";
echo $result;
exit();
}
$sql = "SELECT profilePicture FROM User WHERE useame='$log_useame' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
$avatar = $row[0];
//delete old pic if set
if ($avatar != "") {
$picurl = "users/$log_useame/$avatar";
if (file_exists($picurl)) {
unlink($picurl);
}
}
//move file from temp folder to users folder
$moveResult = move_uploaded_file($fileTmpLoc, "users/$log_useame/$db_file_name");
if ($moveResult != true) {
$result = "File upload failed";
echo $result;
exit();
}
include_once("image_resize.php");
//replace original file with resized version
$target_file = "users/$log_useame/$db_file_name";
$resized_file = "users/$log_useame/$db_file_name";
$wmax = 400;
$hmax = 600;
img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
$sql = "UPDATE User SET profilePicture='$db_file_name' WHERE useame='$log_useame' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
mysqli_close($db_conx);
header("location: userprofileyh.php?u=$log_useame");
$result = "Uploaded successfully";
echo $result;
exit();
}
?>
HTML FORM
$avatar_form .= '<form id="avatar_form" method="post" enctype="multipart/form-data">';
$avatar_form .= '<h1>Change avatar</h1>';
$avatar_form .= '<input type="file" name="avatar" required>';
$avatar_form .= '<p><input type="submit" value="Upload"></p>';
$avatar_form .= '<p id="status"></p>';
$avatar_form .= '</form>';
