Ajax的PHP只需要工作一次

时间:2022-11-22 01:10:23

The code i'm trying to get to work is part of a price list of products from a db. It works almost all of it but i need one ajax to run multiple times, and it does, it even runs the success sentences but when i check the db its like it just ran once... i hope you can help me.

我试图开始工作的代码是数据库产品价格表的一部分。它几乎所有的工作,但我需要一个ajax运行多次,它确实,它甚至运行成功的句子,但当我检查数据库它就像它只运行一次...我希望你能帮助我。

I take 2 values from inputs which are id and amount of the product, and i add them to the list when a button calls the send function, this is that part of the code:

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

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();
        }
    });
}

(I translated some words to english that are originaly in spanish and to make it more readable to you)

(我将一些单词翻译成英文,原文为西班牙文,并使其更具可读性)

So, the cot[] array keeps the product's id and amount, to use them in the next code, which runs when the list is complete and you hit a save button that calls this function:

因此,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");
      };
  }

}

And it all runs fine, here's the PHP:

这一切都运行良好,这是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());

    ?>

The first ajax runs ok, then it also does the one that's inside the while, and it throw all the alerts but when i check the db it just made 1 row and not all it has to.

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

I'm sorry if it's too obvious or something, i've look a lot of questions and answers in this page and i've been trying to fix this for hours but i just dont see it.

我很抱歉,如果它太明显或什么的,我在这个页面看了很多问题和答案,我一直试图解决这个问题几个小时,但我只是没有看到它。

Thank you beforehand.

先谢谢你了。

1 个解决方案

#1


0  

Try to debug the 2nd jquery file via firebug. what the value you return in i

尝试通过firebug调试第二个jquery文件。你回归的价值是什么?

while (j <= i) { .. .. .

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

#1


0  

Try to debug the 2nd jquery file via firebug. what the value you return in i

尝试通过firebug调试第二个jquery文件。你回归的价值是什么?

while (j <= i) { .. .. .

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