如何将sno数据按升序插入数据库?

时间:2022-05-19 15:08:03
function table_insert(lease_ids){                   
  var lease_id=lease_ids+1;
  var table = document.getElementById('table_data123'), 
  rows = table.getElementsByTagName('tr'),
    i, j, cells, customerId;

  for (i = 0, j = rows.length; i < j; ++i) {
    cells = rows[i].getElementsByTagName('td');
    if (!cells.length) {
      continue;
    }
    sno = cells[0].innerHTML;
    start_date = cells[1].innerHTML;
    end_date = cells[2].innerHTML;
    amount = cells[3].innerHTML;

    $.ajax({
      type: "post",
      url: "insert_table_data.php",
      data: {
        sno: sno, 
        start_date: start_date, 
        end_date: end_date, 
        amount: amount, 
        lease_id: lease_id, 
        calendar_type: calendar_type
      },
      success: function(data){}
    });
    alert('success');
  }
}

Here I always get multiple alert boxes coming with success message when I post the data to the database. When the loop take 10 values it will show 10 alert box. But I need only one alert box.

在这里,当我将数据发布到数据库时,总是会收到带有成功消息的多个警报框。当循环取10个值时,将显示10个警告框。但我只需要一个警告框。

1 个解决方案

#1


2  

I rewrote your code a bit, then put the ajax calls into an array. This array is passed to Promise.all(), which can be used to run something after all ajax calls are finished:

我重新编写了您的代码,然后将ajax调用放入一个数组中。这个数组被传递给promisee .all(),在所有ajax调用完成后,该数组可用于运行某些内容:

function table_insert(lease_ids) {
  var ajaxCalls = [];
  document.querySelectorAll('#table_data123 tr').forEach(row => {
    var cells = row.getElementsByTagName('td');
    if (cells.length) {
      ajaxCalls.push(
        $.post("insert_table_data.php", {
          sno: cells[0].innerHTML,
          start_date: cells[1].innerHTML,
          end_date: cells[2].innerHTML,
          amount: cells[3].innerHTML,
          lease_id: lease_ids + 1,
          calendar_type: calendar_type
        }));
    }
  }); // end forEach()
  Promise.all(ajaxCalls).then(() => {
    alert("success");
  });
}

#1


2  

I rewrote your code a bit, then put the ajax calls into an array. This array is passed to Promise.all(), which can be used to run something after all ajax calls are finished:

我重新编写了您的代码,然后将ajax调用放入一个数组中。这个数组被传递给promisee .all(),在所有ajax调用完成后,该数组可用于运行某些内容:

function table_insert(lease_ids) {
  var ajaxCalls = [];
  document.querySelectorAll('#table_data123 tr').forEach(row => {
    var cells = row.getElementsByTagName('td');
    if (cells.length) {
      ajaxCalls.push(
        $.post("insert_table_data.php", {
          sno: cells[0].innerHTML,
          start_date: cells[1].innerHTML,
          end_date: cells[2].innerHTML,
          amount: cells[3].innerHTML,
          lease_id: lease_ids + 1,
          calendar_type: calendar_type
        }));
    }
  }); // end forEach()
  Promise.all(ajaxCalls).then(() => {
    alert("success");
  });
}