Php 提交后ajax刷新表单内容

Php 提交后ajax刷新表单内容,php,jquery,html,Php,Jquery,Html,我正在构建一个页面,您单击一个按钮,它将使用ajax加载div,从而减少加载时间,并使它们保持在同一页面上。当您单击按钮加载带有表单的div时,它会按预期加载表单。我试图让它在用户单击submit后重新加载表单,这样select和DataList都会随着新提交的记录而更新,当您在一个div中输入数据时,当您打开它时,它会填充另一个div中的列表。现在我可以让它第一次加载,但一旦加载了div,它就不会重新加载div了。我在下面放了一些源代码。谢谢你的建议 -呈现给用户的div我有一些jquery可

我正在构建一个页面,您单击一个按钮,它将使用ajax加载div,从而减少加载时间,并使它们保持在同一页面上。当您单击按钮加载带有表单的div时,它会按预期加载表单。我试图让它在用户单击submit后重新加载表单,这样select和DataList都会随着新提交的记录而更新,当您在一个div中输入数据时,当您打开它时,它会填充另一个div中的列表。现在我可以让它第一次加载,但一旦加载了div,它就不会重新加载div了。我在下面放了一些源代码。谢谢你的建议

-呈现给用户的div我有一些jquery可以打开它,所以忽略显示: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>
-填充表单的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 />';

}
-处理表单提交的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>');
    }
}

}   
希望这能让我所做的事情有意义。在我的页面中有很多逻辑,但试图保持它的新鲜,干净和用户友好的前端用户

$('#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.");
        }
    });
});
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>');
    }
}

}