0

In my application i'am making two ajax call for getting some data from database based on another variable value. code part:

var xmlHttp;
var redirect;

function populate_site(obj, passedselect) {
    xmlHttp = GetXmlHttpObject();

    var site_type;

    if (obj.value) {
        site_type = obj.value
    }else {
        site_type = document.app.site_type.value;
    }

    var url="/cgi-bin/Web/Begin.cgi";
    url=url+"?redirect=Get_Site_List";
    url=url+"&site_type=" + site_type;

    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);    
}

function stateChanged() {     
    if (xmlHttp.readyState == 4){ 
        document.getElementById('site_id').innerHTML = xmlHttp.responseText;
    }
}

function Populate_Process_List(obj, process_id_param) {    
    var action_id;

    if (obj.value) {
        action_id = obj.value
    }else {
        action_id = document.app.action_id.value;
    }

    xmlHttp = GetXmlHttpObject();

    if (obj.value == '') {
        document.getElementById('process_id').innerHTML = ' ';
        return false;
    }

    var url="/cgi-bin/Web/Begin.cgi";
    url=url+"?redirect=Fetch_User_Process_List";
    url=url+"&action_id=" + action_id;
    url=url+"&process_id=" + process_id_param;
    url=url+"&gp_flag=" + 'YES';                                         
    xmlHttp.onreadystatechange= Process_Data_StateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);        
}

function Process_Data_StateChanged() {
    if(xmlHttp.responseText != 'NA') {
        if (xmlHttp.readyState == 4){
            document.getElementById('process_id').innerHTML = xmlHttp.responseText;
        }
    }
}

function GetXmlHttpObject(){
    var xmlHttp1=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp1=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp1;
}

And validating site_id and process_id variable using JavaScript.

Problem facing is whenever user press the submit button before ajax call loading is not completed (since me passing async parameter of the open() method has as true). Even while validating the site_id and process_id variable using JavaScript is not getting captured.

Validation code:

var siteaddindex = document.app.site_id[document.app.site_id.selectedIndex].value;

if(siteaddindex == "null"){
    alert("Please select site location.");
    document.app.site_id.focus();
    return false;
}

var process_id = document.app.process_id.value;

if (process_id == '') {
    alert("Please select process");
    return false;
}

Please let me know how to perform validation of above fields, since those fields are mandatory.

user142847
  • 15
  • 6
  • I only skimmed the question, but have you considered using promises? – ndugger Oct 31 '14 at 12:43
  • Enable the button only after load complete – Amal Hashim Oct 31 '14 at 13:58
  • Since in my programming having two ajax calls, how can i come to know whether both ajax calls are loaded completely. so that i can enable the button for submission. – user142847 Nov 03 '14 at 09:24
  • See: http://stackoverflow.com/questions/4631774/coordinating-parallel-execution-in-node-js/4631909?s=8|3.9164#4631909 – slebetman Nov 04 '14 at 07:17
  • Note that these days there is a library, async.js, that implements the answer I've given above + more ways for you to control these sort of things. – slebetman Nov 04 '14 at 07:18

0 Answers0