By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,237 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,237 IT Pros & Developers. It's quick & easy.

Doubt in Ajax: get responseText in main function

P: 58
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().


Expand|Select|Wrap|Line Numbers
  1. //main function
  2. function validateGridAdd(a,rowNum){
  3.     var tForm = document.hallAllotFrm;
  4.         if(!validateCommon()){
  5.             return;
  6.         }
  7.  
  8.         getValidation();    
  9.     var length = document.getElementsByName("arrInv").length; 
  10.         var invArr = document.getElementsByName("arrInv");
  11.         var currInv = document.getElementById("arrInv0");
  12.         var currRel = document.getElementById("arrReliev3");
  13.  
  14.         if(currInv.value==currRel.value){
  15.             alert('Supervisor and  Relieving Faculty cannot be same person.');
  16.             return false;
  17.         }
  18.  
  19.         return true;
  20.     } 
  21.  
  22. //function for ajax
  23. function getValidation(){
  24.  
  25. var tForm = document.hallAllotFrm;
  26.  
  27.         var invig = document.getElementById("arrInv0").value;
  28.         var fromTime = document.getElementById("arrFrom1").value;
  29.         var toTime = document.getElementById("arrTo2").value;
  30.         var examDate = document.getElementById("date").value;
  31.  
  32.         var url = "validateSchedule.action?invigId=" + invig+""+'&fromTime='+fromTime
  33.          +""+'&toTime='+toTime+""+'&examDate='+examDate+""+'&flag='+flag;
  34.  
  35.  populateSelectBoxes(url);
  36.  
  37. }
  38.  
  39. function populateSelectBoxes(url){ 
  40.  
  41.         if (window.XMLHttpRequest) {
  42.                 req = new XMLHttpRequest();
  43.         } else if (window.ActiveXObject) {
  44.           req = new ActiveXObject("Microsoft.XMLHTTP");
  45.         }                                               
  46.         req.open("GET", url, true);
  47.         req.onreadystatechange = populateTextField;
  48.         req.setRequestHeader('Accept','message/x-jl-formresult');
  49.         req.send(null);  
  50. }
  51.  
  52. function populateTextField(){   
  53.     if (req.readyState == 4 && req.status == 200) {                 
  54.                  value=req.responseText;
  55.              if(value=="true"){
  56.                 alert("Supervisor scheduled for class.Please select another supervisor");
  57.                   document.getElementById("arrInv0").value=-1;
  58.                  } 
  59.        }
  60. document.getElementById("flag").value=value;
  61.     } 



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.?
Jun 25 '07 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
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 populateTextField().
Jan 17 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.