2

I'm calling an AJAX using a $.when to wait till that ajax completes and return to process the next ajax inside.

This is where $.when calling happens:

function loadAllData(){
    $.when(getCreditorID()).done(function(a1){
        console.log("cx id is : " + parseFloat(a1[0]));  //this is in the attached screen shot
        var urlx = "functions/getCustomerData.php";
        $.post(
            urlx, 
            { 
                selectedValue: a1[0],
            },
            function(data) {
                $("#payduedate").val(data[0].duedate);
                document.getElementById('portcode').value = data[0].portcode;
                document.getElementById('currencycode').value = data[0].currencycode;
                document.getElementById('convertion').value = data[0].conversion;
            }, 
            "json"
        );
    });
}

Above code is calling below ajax method function:

function getCreditorID(){
    id = "";
    var creditorcodex = document.getElementById('creditorcode').value;
    // console.log("getCreditorID input: " + creditorcodex);
    var urlx = "functions/getCreditorID.php";
    return $.ajax({
        type: 'POST',
        url: urlx,
        data: {
            creditorcode: creditorcodex,
        },
        success: function(data) {
            console.log("Result : "+data);  //this is in the attached screen
        }
    });
}

Above function calling getCreditorID.php to get data: getCreditorID.php:

<?php
    include '../config/dbConn.php';

    $creditorcode = $_POST["creditorcode"];
    // $creditorcode = $_GET["creditorcode"];
    $result="";

    $sql = "SELECT intCustomerID FROM lms.tblcustomers WHERE varCustomerName='".$creditorcode."';";

    mysql_select_db('$dbname');
    $retval = mysql_query( $sql, $conn );

    if(! $retval )
    {
        $result=-999;
    }
    while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
    {
        $result=$row["intCustomerID"];
    }
    echo $result;
    mysql_close($conn);
?>

Problem is: If return from getCreditorID.php is '44' then console.log("Result : "+data); inside getCreditorID() function will output in console as 'Result : 44' and this is working fine. But the same function is getting returned in loadAllData() function and using the value returned for next ajax. Here if we print the return value using console.log("cx id is : " + parseFloat(a1[0])); output is '4' which should be '44'. Which means it's only giving the first character as output and ignoring the rest.

Screenshot of running console: screen shot of running code

Please find a way out.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • Why are you doing `a1[0]` and why do you expect it to not return `4`? The data is `"44"` so `data[0]` would be `4` – apokryfos Oct 13 '16 at 09:57

1 Answers1

0

In your function loadAllData(), use a1 instead of a1[0] and update your code accordingly

function loadAllData(){
    $.when(getCreditorID()).done(function(a1){
        console.log("cx id is : " + parseFloat(a1));  // did correction  here
        var urlx = "functions/getCustomerData.php";
        $.post(
            urlx, 
            { 
                selectedValue: a1[0],
            },
            function(data) {
                $("#payduedate").val(data[0].duedate);
                document.getElementById('portcode').value = data[0].portcode;
                document.getElementById('currencycode').value = data[0].currencycode;
                document.getElementById('convertion').value = data[0].conversion;
            }, 
            "json"
        );
    });
}
Nitin Pund
  • 1,082
  • 2
  • 9
  • 23