weixin_33728708 2019-03-28 16:17 采纳率: 0%
浏览 35

Ajax Div刷新太快

I have a form that posts to a script via ajax, the script inserts the form data into a database and using ajax i the refresh the div on the original page which then shows the form as well as a list of records in the database (from the form input)

I hope that makes sense.

My issue is that sometimes, maybe 1 time in 20, the div refreshes too quickly and doesn't pick up on the new data if i refresh the page its there and if i submit a new record both records are then in the list.

I guess i just need to delay the refreshing but i don't know how.

thanks,

php

echo '<div id="cuttingListDiv">';
$sql = "SELECT * FROM `cuttingList` WHERE jobRef = '".$_SESSION["jobRef"]."' ORDER BY pieceMaterialName, pieceThickness ASC";
$results = $dbconn->query($sql);
if ($results->num_rows > 0) {
    echo '<h3>Cutting List:</h3>';
    echo '<div align="center">';
    while($row = $results->fetch_assoc()) {
        echo '<br />';
        echo '<div class="row">';
        echo '<div class="col" align="center">';
        echo '<h5>'.$row["pieceMaterialName"] . ' - ' . $row["pieceLength"] . '&nbsp;x&nbsp' . $row["pieceWidth"] . '&nbsp;x&nbsp' . $row["pieceThickness"] . 'mm</h5>';
        echo '<br />';
        echo '</div>';
        echo '</div>';
    }
    echo '</div>';
}
echo '</div>';

ajax script

session_start();
include '_script_sqlConnection.php';

$pieceLength = strip_tags($_POST['pieceLength']);
$pieceWidth = strip_tags($_POST['pieceWidth']);
$thickness = strip_tags($_POST['thickness']);
$material = strip_tags($_POST['material']);
$materialName = strip_tags($_POST['materialName']);
$sheetID = strip_tags($_POST['sheetID']);

// Swap width & length id width bigger
if ($pieceWidth > $pieceLength) {
    $tmpDimm = $pieceLength;
    $pieceLength = $pieceWidth;
    $pieceWidth = $tmpDimm;
}

$sql = "INSERT INTO `cuttingList`(`orderRef`, `pieceMaterialName`, `pieceThickness`, `pieceLength`, `pieceWidth`, `sheetID`, `pieceWeight`, `qty`) VALUES ('".$_SESSION["quoteRef"]."', '".$materialName."', '".$thickness."', '".$pieceLength."', '".$pieceWidth."', '".$sheetID."', '".$pieceWeight."', '1')";
if ($dbconn->query($sql) === FALSE) {
    echo "Error Adding Pieces To Cut List.<br />";
}

javascript

$("#cuttingListForm").submit(function(){
    $.ajax({
        type: "POST",
        url: "_script_ajax_addToCuttingList.php",
        data: $('form.cuttingListForm').serialize(),
        success: function() {
            $("#cuttingListDiv").load(location.href + " #cuttingListDiv");
        }
    }); 
});
  • 写回答

2条回答 默认 最新

  • 关注

    Use the JS function setTimeout

    // example of usage for setTimeout: 
    setTimeout( function(){
    $("#cuttingListDiv").load(location.href + " #cuttingListDiv");
    }, 2000 ); 
    

    In the example you have 2000 milliseconds for 2 second wait before fired the call.

    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗