ajax在没有事件的情况下工作.preventDefault();-但如果添加了event.prventDefault(
ajax is working without event.preventDefault(); - but breaks if added event.preventDefault();
我构建了一个AJAX请求,向mySql插入一个新行,并将更新后的数据库作为数组返回。它在没有event.preventDefault();
的情况下工作得很好(并重定向到PHP文件),但当我添加event.preventDefault();
以阻止重定向时,它就坏了。
有什么想法吗?
$("#upload-form").submit(function() {
//event.preventDefault();
window.alert(paintsCatlg.length);
document.querySelector("#upload-form #id").value = paintsCatlg.length;
document.querySelector("#upload-form #src").value = "paintings//" + document.querySelector("#upload-form #exhibition_en").value + "//" + fileName;
window.alert(document.querySelector("#upload-form #id").value);
window.alert(document.querySelector("#upload-form #src").value);
var uploadForm = new FormData();
uploadForm.append("id", $('#id').val());
uploadForm.append("src", $('#src').val());
uploadForm.append("title_en", $('#title_en').val());
uploadForm.append("title_he", $('#title_he').val());
uploadForm.append("exhibition_en", $('#exhibition_en').val());
uploadForm.append("exhibition_he", $('#exhibition_he').val());
uploadForm.append("subjects_en", $('#subjects_en').val());
uploadForm.append("subjects_he", $('#subjects_he').val());
uploadForm.append("keywords_en", $('#keywords_en').val());
uploadForm.append("keywords_he", $('#keywords_he').val());
uploadForm.append("height", $('#height').val());
uploadForm.append("width", $('#width').val());
uploadForm.append("sold", $('#sold').val());
var settings = {
// "async": true,
// "crossDomain": true,
"url": "upload.php",
"method": "POST",
"dataType": 'json',
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": uploadForm
}
$.ajax(settings).success(function(data) {
// alert("Hello! I am an alert box!!");
alert('good');
alert(data);
paintsCatlg = data;
});
});
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "chana_goldberg";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// if(isset($_POST['submit'])) {
// unset($_POST['submit']);
$id = $_POST['id'];
$src = $_POST['src'];
$title_en = $_POST['title_en'];
$title_he = $_POST['title_he'];
$exhibition_en = $_POST['exhibition_en'];
$exhibition_he = $_POST['exhibition_he'];
$subjects_en = $_POST['subjects_en'];
$subjects_he = $_POST['subjects_he'];
$keywords_en = $_POST['keywords_en'];
$keywords_he = $_POST['keywords_he'];
$height = $_POST['height'];
$width = $_POST['width'];
$sold = $_POST['sold'];
$enc_exhibition = mb_convert_encoding($exhibition_en, "ASCII");
$target_dir = "paintings/$enc_exhibition/";
// $src = "paintings/$enc_exhibition/";
if ( ! is_dir($target_dir)) {
mkdir($target_dir);
}
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
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;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF 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.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$exhibition_en = mysql_real_escape_string($exhibition_en);
$stmt = $conn->prepare("INSERT INTO paintings_catalog (src, title_en, title_he, exhibition_en, exhibition_he, subjects_en, subjects_he, keywords_en, keywords_he, height, width, sold) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssssssiiss", $src, $title_en, $title_he, $exhibition_en, $exhibition_he, $subjects_en, $subjects_he, $keywords_en, $keywords_he, $height, $width, $sold);
$stmt->execute();
$query = "SELECT * FROM paintings_catalog ORDER BY id";
$result = $conn->query($query);
// numeric array
$paintsCatlg[] = array(1 =>'');
while ($row = $result->fetch_array(MYSQLI_NUM)){
$paintsCatlg[] = $row;
};
echo json_encode($paintsCatlg);
}
}
您需要使用提供给submit
处理程序函数的事件参数,而不是全局事件对象。试试这个:
$("#upload-form").submit(function(e) { // note 'e' here
e.preventDefault();
// your code...
});
您忘记将事件参数添加到函数中
$("#upload-form").submit(function(event){
//event.preventDefault();
window.alert(paintsCatlg.length);
相关文章:
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 如果选中了多个复选框,如何添加事件
- 如果跨度类文本=1,如何将boxShadow添加到跨度类
- 如果数组仍在执行,async.forEach()将运行我添加到数组中的新元素
- 如果在iframe中查看页面,如何在Wordpress中添加body类
- 如果window.location.href.indexOf('player=1')添加样式
- underline-js:将键对象与数组键对象进行比较,如果存在,则移除现有的添加新的
- 如果存在特定类,请不要添加类
- 如果选中复选框,则 JavaScript 添加值
- 如果选中单选框,则将值添加到合计
- 如果没有从下拉列表中选择任何值,则允许使用php、mysql、javascript将文本框添加到mysql表中
- JSON:loop Invoice Items:如果InvoiceNo未退出则添加,如果退出则更新值,示例包括在内
- 如果选中复选框,则添加样式
- 如果没有添加PHP&Javascript
- jQuery appendTo 替换元素而不是添加元素,如果正在添加的元素预先存在于列表中
- 如果添加了新的数据库记录,则自动更新 HTML 的
- Nodejs 快速路由:如果添加了“/”,则没有样式
- 如果添加了播放列表,JWPLAYER 播放器不起作用
- 角度自动完成仅在删除符号时起作用,但如果添加其他符号则不工作
- ajax在没有事件的情况下工作.preventDefault();-但如果添加了event.prventDefault(