Ajax';让PHP只工作一次


Ajax's PHP into while just working once

我试图开始工作的代码是数据库中产品价目表的一部分。它几乎可以运行所有这些,但我需要一个ajax来运行多次,它确实运行了,它甚至运行了成功语句,但当我检查数据库时,它就像只运行过一次一样。。。我希望你能帮助我。

我从输入中获取两个值,即产品的id和数量,当按钮调用发送函数时,我将它们添加到列表中,这是代码的一部分:

function valores(cod, cant) {
if (cod != '') {
    cot.push([cod, cant]);
    i++;
}
return cot;
}
function send () {
event.returnValue=false; 
var id = $('#id').val();
var amount = $('#cant').val();
var total;

    if ($('#total').length > 0) {
        total = document.getElementById('total').value;
    } else {
        total = 0;
    }
    $.ajax({
        type: 'POST',
        data: ({cod : id, cant : amount, tot : total }),
        url: 'process/agprods.php',
        success: function(data) {
            $('#totals').remove();
            $('#prodsadded').append(data);
            valores(id, amount);
            rs = $(document.getElementById('rs').html);
        },
        error: function () {
            $('#rs').innerHTML = rs;
            document.getElementById('note').innerHTML = "Error: The product doesn't exist.";
            $('#handler-note').click();
        }
    });
}

因此,cot[]数组保留产品的id和数量,以便在下一个代码中使用它们,当列表完成并点击调用此函数的保存按钮时,该代码就会运行:

function ncotiza () {
  event.returnValue=false;
  var nlist = $('#codp').val();
  var day = $('#days').val();
  $.ajax({
    async: false,
    type: 'POST',
    data: ({listnumber: nlist, days : day}),
    url: 'process/ncot.php'
  });
  j = 0;
  while (j <= i) {
      if (cot[j][0] != 0 && cot[j][1] != 0) {
          var num = cot[j][0];
          var cant = cot[j][1];
          $.ajax({
            async: false,
            type: 'POST',
            data: ({ listnumber : nlist, prodid: num, amount : cant }),
            url: 'process/ncotpro.php',
            success: function () {
                alert('Success');
            }
          });
          cot[j][0] = 0;
          cot[j][1] = 0;
          j++;
      }
      if (j == i) {
          window.location.reload(1);
          alert("Finished Successfully");
      };
  }
}

一切运行良好,下面是PHP:

(ncot.php)

    $listnumber = isset($_POST["listnumber"]) ? $_POST["listnumber"] : '';
    $days = isset($_POST["days"]) ? $_POST["days"] : '';
    $cons = "INSERT INTO pricelist (listnumber, diashabiles, cdate)
                VALUES ('$listnumber', '$days', CURDATE())";
    mysql_query($cons);
    ?>

(ncotpro.php)

    $listnumber = isset($_POST["listnumber"]) ? $_POST["listnumber"] : '';
    $prodid = isset($_POST["prodid"]) ? $_POST["prodid"] : '';
    $amount = isset($_POST["amount"]) ? $_POST["amount"] : '';
    $cons = "SELECT price, um
                FROM inventory 
                WHERE listnumber = ".$prodid;
    $result = mysql_query($cons) or die ("Error: ".mysql_error());
    $row=mysql_fetch_assoc($result);
    $umcons = mysql_query("SELECT uvalue FROM um WHERE id = ".$row["um"]) or die ("Error:".mysql_error());
    $umres = mysql_fetch_assoc($umcons);
    $vuum = $umres["uvalue"];
    $fprice = $row["price"] * ($amount * $vuum);
    $cons = "INSERT INTO cotpro (cotizacion, producto, amount, monto)
                VALUES ('$listnumber', '$prodid', '$amount', '$fprice')";
    mysql_query($cons) or die ("Error: ".mysql_error());
    ?>

第一个ajax运行正常,然后它也执行了内部的一个,它抛出了所有警报,但当我检查数据库时,它只做了一行,而不是所有必须做的。

如果它太明显或什么的,我很抱歉,我在这个页面上看了很多问题和答案,我已经试图解决这个问题好几个小时了,但我就是看不到。

事先谢谢。

尝试通过firebug调试第二个jquery文件。你在i 中返回的值是多少

而(j<=i){。。。。.