473,485 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Need Help ASAP! AJAX problem

2 New Member
Hi, I am a newbie in the AJAX world. first of all, take a look at my script first:
assume this script is saved as "myAjax.js"

Expand|Select|Wrap|Line Numbers
  1. //-----------------inside myAjax.js--------------------
  2. function AJAX()
  3. {        
  4.     var isBusy = false;
  5.     var obj = null;
  6.     var url = "";
  7.     var Method = "";
  8.     var param = "";
  9.     var result = "";
  10.     var isReady;
  11.  
  12.     if(window.XMLHttpRequest)
  13.         this.obj = new XMLHttpRequest();
  14.     else
  15.     {
  16.         try
  17.         {this.obj = new ActiveXObject("MSXML2.XMLHTTP");}
  18.         catch(exception)
  19.         {
  20.             try
  21.             {this.obj = new ActiveXObject("Microsoft.XMLHTTP");}
  22.             catch(exception){this.obj = null;}    
  23.         }    
  24.     }
  25.     this.isReady = false;
  26.  
  27.     function setOpenProperties(_method,_url,_param)
  28.     {
  29.         this.url = _url;
  30.         this.Method = _method;
  31.         this.param = _param;        
  32.     }
  33.  
  34.     function getData(data)
  35.     {        
  36.         if(this.obj)
  37.         {            
  38.             if(isBusy)
  39.             {
  40.                 window.status = "busy";
  41.             }                    
  42.             var tObj = this.obj;
  43.             if(this.Method.toUpperCase() == "POST")    
  44.             {
  45.                 this.obj.open(this.Method,this.url);
  46.                 this.obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
  47.             }            
  48.             else
  49.             {
  50.                 if(this.param != "")
  51.                 {this.obj.open(this.Method,this.url + "?" + this.param + data);}
  52.                 else
  53.                 {this.obj.open(this.Method,this.url + "?" + data);}
  54.             }            
  55.             isBusy = true;
  56.             this.obj.onreadystatechange = function()
  57.             {                
  58.                 if(tObj.readyState == 4)
  59.                 {                    
  60.                     if(tObj.status == 200)
  61.                     {                                                                    
  62.                         var temp = tObj.responseText;                        
  63.                         saveResult(temp);                        
  64.                         isBusy = false;                    
  65.                     }    
  66.                 }                
  67.             }                                    
  68.  
  69.             if(this.Method.toUpperCase() == "POST")
  70.             {
  71.                 data = this.param + encode(data);
  72.                 this.obj.send(data);
  73.             }
  74.             else
  75.             {this.obj.send(null);}
  76.         }
  77.     }    
  78.  
  79.     function saveResult(_result)
  80.     {
  81.         this.obj.result = _result;
  82.         this.obj.isReady = true;        
  83.     }
  84.  
  85.     function getResult()
  86.     {    
  87.         alert(this.obj);        
  88.     }
  89.  
  90.     this.setOpenProperties = setOpenProperties;
  91.     this.getData = getData;        
  92.     this.getResult = getResult;
  93. }
//--------------------------End of script---------------------------

this AJAX function is called from another js file, assume myMain.js.
script in myMain.js is pretty simple(like below:

Expand|Select|Wrap|Line Numbers
  1. //--------------Inside myMain.js------------------------
  2. var count = 5;
  3. var obj = new AJAX();
  4.  
  5. function display_thumbnail(htmlObj,approved_flag)
  6. {    
  7.     var str = "select p_thumImage from products where p_gallery = " + approved_flag;        
  8.     obj.setOpenProperties("POST","callBackHandler.php","function=query&type=p_image&sql=")    
  9.     obj.getData(str);
  10.     var isReady = false;
  11.     obj.getResult();    
  12.     //alert(obj.getResult());    
  13. }
//------------------------End of script--------------------------

the function "display_thumbnail" will be called as a part of mouseclick event in one of my html page.

As you can see in myMain.js, I am trying to get any data that is returned from the call by the XmlHttpRequest.responseText method which is called inside getData function declared in myAJAX.js, but I don't know how to do that "properly". the function getResult will return the data properly after the user clicks on the link which will call the "display_thumbnail" function once again. In the other words, the first call to getResult always return nothing.

Can someone help me out with this???
Jun 19 '07 #1
3 1432
acoder
16,027 Recognized Expert Moderator MVP
Welcome to TSDN!

I have added code tags for you.

What is the purpose of line 10 in myMain.js, i.e. setting variable isReady to false?
Jun 19 '07 #2
praveen2gupta
201 New Member
Hi, I am a newbie in the AJAX world. first of all, take a look at my script first:
assume this script is saved as "myAjax.js"

//-----------------inside myAjax.js--------------------
function AJAX()
{
var isBusy = false;
var obj = null;
var url = "";
var Method = "";
var param = "";
var result = "";
var isReady;

if(window.XMLHttpRequest)
this.obj = new XMLHttpRequest();
else
{
try
{this.obj = new ActiveXObject("MSXML2.XMLHTTP");}
catch(exception)
{
try
{this.obj = new ActiveXObject("Microsoft.XMLHTTP");}
catch(exception){this.obj = null;}
}
}
this.isReady = false;

function setOpenProperties(_method,_url,_param)
{
this.url = _url;
this.Method = _method;
this.param = _param;
}

function getData(data)
{
if(this.obj)
{
if(isBusy)
{
window.status = "busy";
}
var tObj = this.obj;
if(this.Method.toUpperCase() == "POST")
{
this.obj.open(this.Method,this.url);
this.obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
}
else
{
if(this.param != "")
{this.obj.open(this.Method,this.url + "?" + this.param + data);}
else
{this.obj.open(this.Method,this.url + "?" + data);}
}
isBusy = true;
this.obj.onreadystatechange = function()
{
if(tObj.readyState == 4)
{
if(tObj.status == 200)
{
var temp = tObj.responseText;
saveResult(temp);
isBusy = false;
}
}
}

if(this.Method.toUpperCase() == "POST")
{
data = this.param + encode(data);
this.obj.send(data);
}
else
{this.obj.send(null);}
}
}

function saveResult(_result)
{
this.obj.result = _result;
this.obj.isReady = true;
}

function getResult()
{
alert(this.obj);
}

this.setOpenProperties = setOpenProperties;
this.getData = getData;
this.getResult = getResult;
}
//--------------------------End of script---------------------------

this AJAX function is called from another js file, assume myMain.js.
script in myMain.js is pretty simple(like below:

//--------------Inside myMain.js------------------------
var count = 5;
var obj = new AJAX();

function display_thumbnail(htmlObj,approved_flag)
{
var str = "select p_thumImage from products where p_gallery = " + approved_flag;
obj.setOpenProperties("POST","callBackHandler.php" ,"function=query&type=p_image&sql=")
obj.getData(str);
var isReady = false;
obj.getResult();
//alert(obj.getResult());
}
//------------------------End of script--------------------------

the function "display_thumbnail" will be called as a part of mouseclick event in one of my html page.

As you can see in myMain.js, I am trying to get any data that is returned from the call by the XmlHttpRequest.responseText method which is called inside getData function declared in myAJAX.js, but I don't know how to do that "properly". the function getResult will return the data properly after the user clicks on the link which will call the "display_thumbnail" function once again. In the other words, the first call to getResult always return nothing.

Can someone help me out with this???
I think you are not able to display the resturned data / text. The ajax returned is dispalyed in the div tag in the page. You should create an div tag where you wants to display the returned text.
I am explaning the three major pars of the as separately.
Plese take help of following code and set in your application. The
<div id="targetDiv">
<p>The fetched data will go here.</p>
</div> is the location where result will be placed.

function getData(dataSource, divID) , function defined the div where you wants
to display the result.

The onmouseover events is calling function getData is defining the name of div tag where result should br returned.

<input type="button" value="Display Message" onmouseover='getData("test.txt","targetDiv")'>

please take the help of following code


Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head>
  4. <title>Ajax at work</title>
  5.  
  6. <script language = "javascript">
  7. var httpObj = false;
  8.  
  9.     if (window.XMLHttpRequest) 
  10.     {
  11.         httpObj = new XMLHttpRequest();
  12.     } 
  13.     else if (window.ActiveXObject) 
  14.     {
  15.         httpObj = new ActiveXObject("Microsoft.XMLHTTP");
  16.     }
  17.  
  18.  
  19.  
  20.  
  21.     function getData(dataSource, divID)
  22.     {
  23.         if(httpObj) 
  24.         {
  25.             var obj = document.getElementById(divID);
  26.             httpObj.open("GET", dataSource,true);
  27.             httpObj.onreadystatechange = function()
  28.             {
  29.                 if (httpObj.readyState == 4 && httpObj.status == 200) 
  30.                 {
  31.                     obj.innerHTML = httpObj.responseText;
  32.                 }
  33.             }
  34.             httpObj.send(null);
  35.         }
  36.     }
  37. </script>
  38. </head>
  39. <body>
  40. <H1>Fetching data with Ajax</H1>
  41.  
  42. <form>
  43. <input type="button" value="Display Message" onclick="getData(options1.php','targetDiv')">
  44. <input type="button" value="Display Message" onmouseover='getData("test.txt","targetDiv")'>
  45.  
  46. </form>
  47.  
  48. <div id="targetDiv">
  49. <p>The fetched data will go here.</p>
  50. </div>
  51. </body>
  52. </html>
  53.  
  54.  
Jun 19 '07 #3
nicksname7
2 New Member
I've recently updated my code and it will look like this:(in myMain.js)

------------------------------Beginning of myMain.js-----------------------------------
function get_thumbnail(htmlObj,approved_flag)
{
var str = "select p_thumImage from products where p_gallery = " + approved_flag;
obj.setOpenProperties("POST","callBackHandler.php" ,"function=query&type=p_image&sql=")
obj.getData(str);
setTimeout(function(){display_thumbnail(htmlObj,ap proved_flag);},0); //<-- need to use setTimeout to get the result
htmlObj.className = "";
htmlObj.innerHTML = (approved_flag == false)?"Unapproved images":"Approved Images";
htmlObj.onclick = function(){};
htmlObj.onmouseover = function(){};
htmlObj.onmouseout = function(){};
}

function display_thumbnail(htmlObj,isApproved)
{
var p_images = obj.getResult();
}
-------------------------------------End of myMain.js--------------------------------------------

and for the myAjax.js, it will be look like this:

-----------------------------------Beginning of myAjax.js--------------------------------------
function AJAX()
{
var isBusy = false;
var obj = null;
var url = "";
var Method = "";
var param = "";
var result = "";
var tempStorage = "images";

//beginning of constructor part
if(window.XMLHttpRequest)
this.obj = new XMLHttpRequest();
else
{
try
{this.obj = new ActiveXObject("MSXML2.XMLHTTP");}
catch(exception)
{
try
{this.obj = new ActiveXObject("Microsoft.XMLHTTP");}
catch(exception){this.obj = null;}
}
}
this.isReady = false;
//end of constructor part

function setOpenProperties(_method,_url,_param)
{
this.url = _url;
this.Method = _method;
this.param = _param;
}

function getData(data)
{
if(this.obj)
{
if(isBusy)
{
this.obj.onreadystatechange = function(){};
this.obj.abort();
}
var tObj = this.obj;
if(this.Method.toUpperCase() == "POST")
{
this.obj.open(this.Method.toUpperCase(),this.url,t rue);
this.obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
}
else
{
if(this.param != "")
{this.obj.open(this.Method.toUpperCase(),this.url + "?" + this.param + data);}
else
{this.obj.open(this.Method.toUpperCase(),this.url + "?" + data);}
}
isBusy = true;
this.obj.onreadystatechange = function()
{
if(tObj.readyState == 4)
{
if(tObj.status == 200)
{
//saveResult(tObj.responseText);
saveResult(tObj.responseText);
isBusy = false;
}
}
}

if(this.Method.toUpperCase() == "POST")
{
data = this.param + encode(data);
this.obj.send(data);
}
else
{this.obj.send(null);}
}
}

function saveResult(rslt)
{
this.obj.result = rslt;
}


function getResult()
{
//return document.getElementById(tempStorage).value;
//window.status = this.result;
return this.result;
}

this.setOpenProperties = setOpenProperties;
this.getData = getData;
this.getResult = getResult;
}
-----------------------------------------End of myAjax.js--------------------------------------

Somehow I figured out the problem yesterday, and it was related to the timing.
If you take a look in "myMain.js" script, I am trying to overcome the timing problem by using the "setTimeout" function; However, I don't really know when the "onreadystatechange" in myAjax.js function will get the value first before I call the "getResult()" function.

The "saveResult(rslt)" function in myAjax.js is mainly used to save any fetched data resulting from the call to the server database which is triggered inside the "onreadystatechange" function.

let's make it simple through the following example:
suppose I have 2 links in my html file:
getData1 and getData2
Assumably, getData1 and getData2 will return a string value of "myData1" and "myData2", respectively. On this scenario I will click the "getData1" link first before "getData2" link.
the first result by clicking the "getData1" returned "undefined", followed by "myData1" as a result generated by clicking the "getData2".

based on that scenario, you can see that during the first call, the getResult() function wasn't actually ready with the value to be returned to the caller.
my question here is, how do I actually notify the caller whether the result has been fetched or not??
Jun 20 '07 #4

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

Similar topics

4
2196
by: Philip D Heady | last post by:
I'm in need of a good PHP programmer to help me with my lodgings website asap. Someone who can work on it during the day for a few hrs while I'm at work would be great. Prefer they live in USA...
1
1419
by: whisper | last post by:
Medium/small site with mod_python and sqllite or mySQL running Python 2.3.3 or later on Apache 2.x. I can do the python and CGI, but might need a _little_ hand holding for the rest. Will also...
2
1176
by: Pankaj Jain | last post by:
I have to store the icons in System.Windows.Forms.ImageList.I have to read icons from a icon directory... Which class from System.Drawing is best suited for Working with the images(icon)? ...
19
1685
by: wcfinvader | last post by:
I have known this girl forever every since 3rd grade we are both in 10th grade now and I feel like I am in love with her and I want to ask her out but I dont know if I should call her tonight or...
0
1046
by: SQLJunkie | last post by:
Please refer to the following URL for more info... ...
4
1676
by: briggsie2006 | last post by:
I need to write a program called wc.py. It promts the user to input a filename and outputs the number of lines, words, and, characters in the file. I really need help on this because I don't know...
2
1548
by: archana | last post by:
Hi all, I want to send webrquest throguh anonymous proxy. Can anyone tell me how to validate ip address and port number of anonynmous proxy server. Is it using TctpClient class ? And...
5
1521
by: Karthik D V | last post by:
Hello All, I have a table like this ID CHARACTER ----------- --------- 1 A 2 A 3 B 4 B
5
2192
by: byrnes | last post by:
We are looking to immediately recruit programmers for a project; we are interested in obtaining proposals ASAP. Will offer favorable compensation. We need immediately, an individual to complete...
2
1250
by: mirzaali | last post by:
Hi All, I neeed to dispaly the data in html table tag Query :-select comp,total,funds from table1, WHICH WIL RETURN 3-RECORDSETS rs_query(0) rs_query(1) rs_query(2)
1
6825
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7275
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5418
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4857
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4551
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1376
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
247
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.