以下是我对main.php文件的大致看法。一个表单,后面是数据显示的位置,然后是javascript:
<form>
<center><h3>Add new person:</h3></center>
<div class="errors"></div>
<input id="nameOfFruit" class="form-control" type="text" placeholder="Name" />
<select id="typeOfFruit" class="selectpicker" data-width="100%">
<option data-name="99">Select Fruit...</option>
<option data-name="1">Tomato</option>
<option data-name="2">Banana</option>
<option data-name="3">Grape</option>
</select>
<select id="nOfFruit" class="selectpicker" data-width="100%">
<option data-name="99"># of Fruit...</option>
<option data-name="1">1</option>
<option data-name="2">2</option>
<option data-name="3">3</option>
</select>
<a href="#" class="btn" id="addFruit">ADD</a>
</form>这个选择器是Silvio站点上的引导添加。接下来,我有一些代码,其中显示这些数据的表,并从数据库中读取这些表。最后,我有了javascript:
<script>
$(document).ready(function () {
$('#wlFloorplans').selectpicker();
$('#wlBedrooms').selectpicker();
});
jQuery('#addFruit').click(function () {
var $opt = $("#typeOfFruit option:selected");
var fruitString = $opt.attr("data-name");
var str = "name=" + jQuery("#nameOfFruit").val()
+ "&type=" + fruitString
+ "&number=" + jQuery("#nOfFruit").val();
jQuery.ajax({
type: "post",
url: "/adding_fruit.php",
data: str,
dataType: "json",
success: function(result) {
if (result.success == 1) {
$(document).ajaxStop(function () { location.reload(true); });
$('#typeOfFruit').selectpicker();
$('#nOfFruit').selectpicker();
}
else {
jQuery(".errors").html(result.errors);
}
}
});
});
</script>问题是,每次我填写这张表格时,都会有两件事发生:
当我第一次填充它时,它将通过ajax调用,而不将任何数据放在数据库中。下一次我试的时候,它会成功的。第一次尝试填写表单时,它会正常工作(所有数据都进入数据库,输出到表上),但是第二次,当我按add按钮时,所有字段都会自己删除,任何内容都不会放到DB中。
编辑:我的adding_fruit.php文件:
<?php
try {
//DB SETUP
}
catch(PDOException $ex) {
}
$name = trim(isset($_POST['name']) ? $_POST['name'] : '');
$type = trim(isset($_POST['type']) ? $_POST['type'] : '');
$number = trim(isset($_POST['number']) ? $_POST['number'] : '');
$errors = Array();
if (sizeof($_POST) > 0) {
if ($name === '') {
$errors[] = '<div class="alert" role="alert">NEED A NAME</div>';
}
}
if (sizeof($errors) > 0 || sizeof($_POST) == 0) {
$result = array(
"errors" => implode("", $errors),
"success" => 0);
die(json_encode($result));
}
$randID = md5(uniqid(rand(), true));
$sql = "INSERT INTO waitlist (id, name, type, number)
VALUES ('".$randID."', '".$name."', '".$type."', '".$number."')";
$sth = $db->prepare($sql);
$sth->execute();
$result = array(
"success" => 1);
die(json_encode($result));
?>你看到我的代码有问题了吗?
发布于 2016-02-02 20:07:32
在使用ajax时,不应该重新加载整个页面。这就是使用ajax的全部要点(您不必重新加载页面)。我认为您正在寻找的是清除表单(重置到其原始状态)后,用户已提交和服务器响应。
尝试如下:
jQuery
jQuery.ajax({
type: "post",
url: "/adding_fruit.php",
data: str,
dataType: "json",
success: function(result) {
if (result.success == 1) {
$('form').trigger("reset"); //Reset to the original state of the form
$('#typeOfFruit').selectpicker();
$('#nOfFruit').selectpicker();
}
else {
jQuery(".errors").html(result.errors);
}
}
});这个
$(document).ajaxStop(function () { location.reload(true); });不需要,因为ajaxStop是在ajax不依赖于进行ajax调用时执行的,而不是针对这样的特定调用执行的。从文件中:
“每当Ajax请求完成时,jQuery都会检查是否还有其他未决的Ajax请求。如果没有任何请求,jQuery将触发ajaxStop事件。”
当您的代码中到达done()时,ajax就“完成”了您的特定调用,因此不需要在那里使用ajaxStop()。因此,如果要在调用后重新加载整个页面,只需执行以下操作:
//This would reload the whole page
//(just as you were hitting the reload-button in the browser)
location.reload(true); php:
die(json_encode($result));说要结束应用程序,但真正想要的是返回数据:
echo json_encode($result);
return;https://stackoverflow.com/questions/35141427
复制相似问题