目前我在用户上传照片的表单上使用AJAX,然后进入php检查文件,并根据成功与否返回消息。我将结果回显到段落标记中,所以此刻我的PHP会将用户标题到我想要的页面,但是它会在标签内容中加载页面,我在回显结果。
我无法真正使用AJAX的成功来做到这一点,因为即使向用户显示错误消息它也是成功的
这是我的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 username='$log_username' 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_username/$avatar";
if (file_exists($picurl)) {
unlink($picurl);
}
}
//move file from temp folder to users folder
$moveResult = move_uploaded_file($fileTmpLoc, "users/$log_username/$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_username/$db_file_name";
$resized_file = "users/$log_username/$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 username='$log_username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
mysqli_close($db_conx);
header("location: userprofileyh.php?u=$log_username");
$result = "Uploaded successfully";
echo $result;
exit();
}
?>
HTML表格
$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>';
答案 0 :(得分:1)
如果上传失败,您可以通过在php中抛出异常来使用错误块。
例如,而不是
SELECT * FROM `mrbs_entry`
WHERE DATE_SUB(FROM_UNIXTIME(`end_time`), INTERVAL 14 HOUR) = FROM_UNIXTIME(`start_time`);
你做了
if ($moveResult != true) {
$result = "File upload failed";
echo $result;
exit();
}
并在前端抓住它:
if ($moveResult != true) {
throw new Exception();
}
答案 1 :(得分:1)
也许尝试从php脚本中传回更多关于它是否真的成功的信息。然后在ajax select中编写自己的逻辑,以解析它是否成功或是否发生错误。然后你也可以用它来做重定向。
示例:
<?php
$result['err'] = 'error';
$result['msg'] = 'File upload failed';
echo json_encode($result);
?>
然后用jQuery解析JSON。