Doubt in Ajax: get responseText in main function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shiniskumar
    New Member
    • Feb 2007
    • 58

    Doubt in Ajax: get responseText in main function

    i go tto perform some validation while ck=liking a button without refreshing the value.. i used ajax.My code is as follows...on button click i call the function validateGridAdd ();
    Inside tat function i need to call the function tat uses ajax to go to the server and come back.. the resultant value is to be obtained in the main function validateGridAdd ().


    [CODE=javascript]//main function
    function validateGridAdd (a,rowNum){
    var tForm = document.hallAl lotFrm;
    if(!validateCom mon()){
    return;
    }

    getValidation() ;
    var length = document.getEle mentsByName("ar rInv").length;
    var invArr = document.getEle mentsByName("ar rInv");
    var currInv = document.getEle mentById("arrIn v0");
    var currRel = document.getEle mentById("arrRe liev3");

    if(currInv.valu e==currRel.valu e){
    alert('Supervis or and Relieving Faculty cannot be same person.');
    return false;
    }

    return true;
    }

    //function for ajax
    function getValidation() {

    var tForm = document.hallAl lotFrm;

    var invig = document.getEle mentById("arrIn v0").value;
    var fromTime = document.getEle mentById("arrFr om1").value;
    var toTime = document.getEle mentById("arrTo 2").value;
    var examDate = document.getEle mentById("date" ).value;

    var url = "validateSchedu le.action?invig Id=" + invig+""+'&from Time='+fromTime
    +""+'&toTime='+ toTime+""+'&exa mDate='+examDat e+""+'&flag='+f lag;

    populateSelectB oxes(url);

    }

    function populateSelectB oxes(url){

    if (window.XMLHttp Request) {
    req = new XMLHttpRequest( );
    } else if (window.ActiveX Object) {
    req = new ActiveXObject(" Microsoft.XMLHT TP");
    }
    req.open("GET", url, true);
    req.onreadystat echange = populateTextFie ld;
    req.setRequestH eader('Accept', 'message/x-jl-formresult');
    req.send(null);
    }

    function populateTextFie ld(){
    if (req.readyState == 4 && req.status == 200) {
    value=req.respo nseText;
    if(value=="true "){
    alert("Supervis or scheduled for class.Please select another supervisor");
    document.getEle mentById("arrIn v0").value=-1;
    }
    }
    document.getEle mentById("flag" ).value=value;
    } [/CODE]




    ive got a hidden variable "flag" in my code.. tat variable is set to the responseText in my function. i need to get the responseText in my main function validateGridAdd (a,rowNum);
    the value of flag can be eithr true or false.
    How do i get this value in the main function.?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Once you've set the flag, it should be available in the main function though with the asynchronous nature of things, the question is when. The best thing would be to just put the code that depends on this variable setting in populateTextFie ld().

    Comment

    Working...