简体   繁体   English

AJAX 不断显示来自数组的错误数据

[英]AJAX keep showing wrong data from array

I have a loop that calls multiples AJAX to find out if there's any booking in the database or not.我有一个循环调用多个 AJAX 来确定数据库中是否有任何预订。 JS will pass the data from an array to AJAX and find it out in database through SQL query. JS 将数组中的数据传递给 AJAX 并通过 SQL 查询在数据库中查找。

However, the data returned from the AJAX is correct, and if it's there in database, i want to to show the data returned from AJAX and the current value of array in current loop, but still the data that i show from the array is the last index of array.但是,从 AJAX 返回的数据是正确的,如果它在数据库中,我想显示从 AJAX 返回的数据和当前循环中数组的当前值,但我从数组中显示的数据仍然是数组的最后一个索引。

javascript: javascript:


function getButtonInfo() {
    var jam = [7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22];
    var lap = ['Lapangan A','Lapangan B','Lapangan Batminton'];
    var lapId = ['lapA','lapB','lapBat'];
    for (var j = 0; j < lap.length; j++){
      for (var i = 0;i < jam.length; i++){
        var lapIdFix = jam[i]+lapId[j];
        var lapId2 = jam[i]+lap[j];
        var lap1 = lap[j];
        if(jam[i] < 10){
          var jamFix = '0'+jam[i]+':00:00'; 
        }else{
          var jamFix = jam[i]+':00:00';
        }

        $.ajax({
          type: "POST",
          url:'get-button-avail-ajax.php',
          data: {
            date: document.getElementById('tgllapA').value,
            time: jamFix,
            lapangan: lap[j]
          },
          complete: function (response) {
            if(response.responseText != "0"){
              document.getElementById(lapIdFix).disabled = true;
              $('#output').html(response.responseText );
              $('#output1').html(lapIdFix);
              $('#output2').html(lapId2);
            }else{
              $('#output3').html(response.responseText);
            }
            //$('#output').html(response.responseText);*
          },
          error: function () {
              $('#output').html('ERROR!');
          },
        });
      }
    }
    return false;
    }

PHP File: PHP 文件:

<?php

    ob_start();
    $error=""; // Variable To Store Error Message
    $connection = mysqli_connect(/*credential*/);
    $tgl = $_POST['date'];
    $time = $_POST['time'];
    $lap = $_POST['lapangan'];

    //Query
    $query = mysqli_query($connection, "SELECT * FROM lapangan_book WHERE tanggal='$tgl' and jam='$time' and lapangan='$lap'");
    $rows = mysqli_num_rows($query);
    $data = mysqli_fetch_array($query);

    if($rows > 0){
        echo $data['lapangan'];
    }else{
        echo "0";
    }

?>

The output should be output 应该是

Lapangan A
22lapA
22Lapangan A

But keep showing但继续显示

Lapangan A
22lapBat
22Lapangan Batminton

Yes, this is happening because of the Asyncroniouse behavior of ajax.是的,这是因为 ajax 的异步行为而发生的。 There is two tricks you have to send asynchronous request by async: false or you have to call the recursive function after success response from ajax request.您必须通过async: false或者您必须在 ajax 请求的成功响应后调用递归 function。

Trick 1- Pass option aysnc: false in ajax request, but some browser will throws warning in synchronous request of ajax Trick 1- Pass option aysnc: false in ajax 请求,但某些浏览器会在 ajax 的同步请求中抛出警告

        $.ajax({
          type: "POST",
          url:'get-button-avail-ajax.php',
          async:false,
          data: {
            date: document.getElementById('tgllapA').value,
            time: jamFix,
            lapangan: lap[j]
          },
          complete: function (response) {
            if(response.responseText != "0"){
              document.getElementById(lapIdFix).disabled = true;
              $('#output').html(response.responseText );
              $('#output1').html(lapIdFix);
              $('#output2').html(lapId2);
            }else{
              $('#output3').html(response.responseText);
            }
            //$('#output').html(response.responseText);*
          },
          error: function () {
              $('#output').html('ERROR!');
          },
        });
      }

Trick 2: Recursive function, this is most accurate way of calling技巧2:递归function,这是最准确的调用方式

function getButtonInfo() {
    var jam = [7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22];
    var lap = ['Lapangan A','Lapangan B','Lapangan Batminton'];
    var lapId = ['lapA','lapB','lapBat'];
    var i=0;
    var j=0;
    var ajaxCall= function(){
        var lapIdFix = jam[i]+lapId[j];
        var lapId2 = jam[i]+lap[j];
        var lap1 = lap[j];
        if(jam[i] < 10){
          var jamFix = '0'+jam[i]+':00:00'; 
        }else{
          var jamFix = jam[i]+':00:00';
        }

        $.ajax({
          type: "POST",
          url:'get-button-avail-ajax.php',
          async:false,
          data: {
            date: document.getElementById('tgllapA').value,
            time: jamFix,
            lapangan: lap[j]
          },
          complete: function (response) {
            if(response.responseText != "0"){
              document.getElementById(lapIdFix).disabled = true;
              $('#output').html(response.responseText );
              $('#output1').html(lapIdFix);
              $('#output2').html(lapId2);
            }else{
              $('#output3').html(response.responseText);
            }
            //$('#output').html(response.responseText);*
            var recursiveCall=true;
            i=i+1;
            if(i>=jam.length){
                j=j+1;
                if(j>=lap.length) recursiveCall= false;
                else i=0;
            }
            if(recursiveCall===true)
            {
                 ajaxCall();
            }
          },
          error: function () {
              $('#output').html('ERROR!');
          },
        });
    }
    ajaxCall();
    return false;
}

I have written code for your understanding might be your have to made come modification in this code我已经编写了代码供您理解可能是您必须在此代码中进行修改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM