duanjian9148 2016-04-12 11:54
浏览 8

$ .post到PHP文件 - 回调没有被击中,直到包含循环结束?

I'm making a stock table, where the user can input a number of stock to issue out, and this function is to verify, for each member of the table, whether or not the quantity of stock in the database is sufficient to make the issue.

When I breakpoint through the code on Chrome, it seems to never hit the inside code of the $.post until after its finished looping through the table. And by then the data that is supposed to be passed to AddIssue just becomes 1 and 10 and nothing else.

It's starting to drive me mad, so I'd appreciate a pointer on what I'm doing wrong here

var issueArray =[];

function VerifyIssue()
{
    var numRows = document.getElementById("searchTable").rows.length - 2;
    var running = true;

    var str1 = "issue_";

    while(running == true)
    {
        if (numRows < 0 && running == true)
        {
            running = false;
            //insert code for success
            break;
        }
        else if (running == false)
        {
            break;
        }

        var str2 = numRows;
        var comb = str1.concat(str2);
        var issue_element = document.getElementById(comb);
        var q = issue_element.value;


        var i = issue_element.name;
        var idd = i.replace("issue_", "");
        var trimId = idd.trim();

        $.post
        (
            'includes/issueCheck.php',
            {
                id: trimId, 
                issueQuantity: q 
            },
            function(result)
            {
                alert(result);

                if (result < 0)
                {
                    alert("One of more quantities inputted are greater than held in stock");
                    running = false;
                }
                if (result > 0)
                {
                    addIssue(trimId, q);
                }
            }
        );
        numRows = numRows - 1;
    }
    alert(issueArray);
}

function addIssue(issueID, quant)
{
    var item = {};

    item.label = issueID;
    item.value = quant;
    issueArray.push(item);

}

This is the PHP that is called by the $.post

<?php
    $server = 'SQL2008';
    $connectionInfo = array( "Database"=>"rde_470585");
    $conn = sqlsrv_connect($server,$connectionInfo);

    $false = -1;
    $true = 1;
    $id = $_POST['id'];
    $quantity = $_POST['issueQuantity'];
    $query = "Select Quantity FROM Stock WHERE StockID = ?";
    $param = array($id);
    $res = sqlsrv_query($conn, $query, $param);
    $row =  sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC);
    $dbQuantity = $row['Quantity'];

    if ($dbQuantity < $quantity)
    {
        echo $false;
    }
    else if($dbQuantity >= $quantity)
    {
        echo $true;
    }
  • 写回答

2条回答 默认 最新

  • dousheng3364 2016-04-12 12:37
    关注

    Do you realise that your function(result){ ... } will only be called when the server response for your POST request is received? You have to build the code to take that delay into account.

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line