我正在尝试上传图片而不刷新页面。我使用event.preventDefault()
来阻止页面刷新。但是,当我将event.preventDefault()
写入我的jQuery代码时,我的jQuery代码中的PHP代码没有被执行(我认为,至少图片没有被上传到mySQL)。这是我的代码:
<?php
$target_file = ($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
else {
$link = mysqli_connect(); // Connects successfully
$image = addslashes(file_get_contents($_FILES['fileToUpload']['tmp_name']));
$image_name = addslashes($_FILES['fileToUpload']['name']);
$query = "INSERT INTO `pictures` (`name`, `image`) VALUES ('{$image_name}', '{$image}')";
if (!mysqli_query($link, $query)) { // Error handling
echo "Something went wrong! :(";
}
else {
$storeArray = Array();
$query = "SELECT image FROM pictures";
if($result = mysqli_query($link, $query)) {
while($row = mysqli_fetch_array($result)) {
$storeArray[] = $row['image'];
}
}
else {
echo "something is wrong!";
}
}
for ($x = 0; $x <= 10; $x++) {
echo '<img src="data:image/jpeg;base64,'.base64_encode($storeArray[$x]).'"/>';
}
}
}
else {
}
?>
event.preventDefault();
});
});
有人知道如何阻止页面刷新并且我的PHP代码正在执行吗?