我正在构建一个单击按钮的页面,它使用ajax加载div(减少加载时间,并将它们保存在同一页面上)。当您单击按钮以使用表单加载div时,它会按预期加载表单。一旦用户点击提交,我试图让它重新加载表单,以便select和datalists都用新提交的记录更新(当你在一个div中输入数据时,它会在你打开它时填充另一个中的列表) 。现在我可以让它第一次加载,但一旦它加载div它不会重新加载div。我在下面放了一些源代码。我很感激任何建议!
- 呈现给用户的div(我有一些打开它的jquery,所以忽略display:none)
<div id="chkitemcntdiv" style="display:none; position: absolute; background-color: whitesmoke; padding: 2% 9% 4% 9%; z-index: 99; top: 26%; left: 40%; border: solid;">
<button class="btn btn-danger pull-right" id="chkitemcntonlyclosebtn" style="border:solid; border-style: outset;position: absolute; top: 2%; left: 92%;"> x </button><br /><br /><br />
<form id="chkitemcntform">
<div id="chkitemcntdivcontent">
</div>
<button type="submit" class="btn btn-primary"> Checkout Item(s) </button>
</form>
</div>
- 加载表单div内容
$('#chkitemcntonlybtn').click(function(){
$.ajax({
url: "/invman/invman_main_ajax.php?ajaxdiv=chkitemcntdiv",
success:
function (data) {
$('#chkitemcntdivcontent').html(data);
},
error: function(data) {
alert("A page control error has occurred. Please contact your systems administrator.");
}
});
});
- 填充表单的php
IF ($_GET['ajaxdiv'] == 'chkitemcntdiv'){
echo' <div class="box-header">
<b class="pull-center">Check Out Count Only Non-Trackable Item</b><br /> <br />
</div>
<label for="source">Source</label>
<select class="form-control" name="source" id="source">';
$sql = "SELECT * FROM invman.locations where status = 1 and type in (SELECT loctype FROM invman.location_logic where logitem = 7 and location_logic.status = 1);";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row['lid'].'">'.$row['description'].'</option>';
}
} else {
echo "<option> Error</option>";
}
echo' </select><br />
<label for="destination">Destination</label>
<select class="form-control" name="destination" id="destination">';
$sql = "SELECT * FROM invman.locations where status = 1 and type in (SELECT loctype FROM invman.location_logic where logitem = 8 and location_logic.status = 1);";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row['lid'].'">'.$row['description'].'</option>';
}
} else {
echo "<option> Error</option>";
}
echo' </select><br />
<label for="itemtype">Item Type</label>
<select class="form-control" name="itemtype" id="itemtype">';
$sql = "SELECT * FROM invman.itemtypes where category = 2 and status = 1;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row['itemtypeid'].'">'.$row['itemtype'].'</option>';
}
} else {
echo "<option> Error</option>";
}
echo ' </select><br />
<label for="count">Quantity</label>
<input type="number" class="form-control" name="count" id="count" min="1"><br />';
}
- JQUERY表单处理程序
$('#chkitemcntform').submit(function(e){
e.preventDefault();
$.ajax({
url: "/jsonsubmit/chkitemcnt.php",
type: "post",
data: $('#chkitemcntform').serialize(),
success: function(data) {
$('#formreturn').html(data);
$('#formreturn').slideToggle();
$( '#chkitemcntform' ).each(function(){
this.reset();
});
setTimeout(function(){
$('#formreturn').fadeToggle(1500);
}, 7000);
},
error: function(data) {
alert("There was an error checking out your count only item. Please contact your system administrator.");
}
});
});
- 处理表单提交的php。
if (!EMPTY($_POST)){
require("the connection string i have removed");
$cleandestination = mysqli_real_escape_string($conn, $_POST['destination']);
$cleansource = mysqli_real_escape_string($conn, $_POST['source']);
$cleanitemtype = mysqli_real_escape_string($conn, $_POST['itemtype']);
$cleanquantity = mysqli_real_escape_string($conn, $_POST['count']);
$transferid = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 200);
$sql = "SELECT * FROM invman.current_countonly_quantity_by_location where lid = '".$cleansource."' and itemtypeid = '".$cleanitemtype."';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
}
} else {
exit('<div class="alert alert-danger alert-dismissible">The quantity entered exceeds the quantity currently in inventory for the source. Please review source inventory or reduce your quantity.</div>');
}
$sql = "SELECT * FROM invman.current_countonly_quantity_by_location where lid = '".$cleansource."' and itemtypeid = '".$cleanitemtype."' and quantity < '".$cleanquantity."';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
exit('<div class="alert alert-danger alert-dismissible">The quantity entered exceeds the quantity currently in inventory for the source. Please review source inventory or reduce your quantity.</div>');
}
} else {
$sql = "INSERT INTO invman.cntitemslocations (itemtype, quantity_transfered, transferid, locid, movedate) VALUES ('".$cleanitemtype."','".$cleanquantity."','".$transferid."','".$cleandestination."', now());";
if (mysqli_query($conn, $sql)) {
} else {
exit('<div class="alert alert-danger alert-dismissible">A database error occurred updating your record. Please contact your systems administrator.</div>');
}
$sql = "INSERT INTO invman.cntitemslocations (itemtype, quantity_transfered, transferid, locid, movedate) VALUES ('".$cleanitemtype."',-'".$cleanquantity."','".$transferid."','".$cleansource."', now());";
if (mysqli_query($conn, $sql)) {
echo '<div class="alert alert-success alert-dismissible">Item(s) checked out successfully.</div>';
} else {
exit('<div class="alert alert-danger alert-dismissible">A database error occurred updating your record. Please contact your systems administrator.</div>');
}
}
}
希望这对我想做的事情有意义。我的页面中有很多逻辑,但试图让前端用户保持新鲜,干净和用户友好。