User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 455,967 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,741 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1978 | Replies: 2
Reply
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

ajax refresh??

  #1  
Nov 22nd, 2007
I'm having some problem trying to load my results into a dedicated span/div.
I found that the sequence of ajax execution is random! I need its order to be correct
so that i can show a list of 'subjects' with latest added item.

in a html hyperlink, i'm calling this in sequence :

insertNewText();loadSubjectList();


Below are my codes and output, it contains,

1.) my insert code
2.) my display code
3.) my php code
4.) my logger output which shows the randomness happening

[hr]


1.) my insert code :

 		function insertNewText(){
		
		if(validate(document.frmNote.txtSubject, 'Subject')){
			try
			{
						
			var xmlHttp = GetXmlHttpObject();
			var url = null;
			var param = null;
			
			   xmlHttp.onreadystatechange=function()
			      {
			      if(xmlHttp.readyState==4)
			        {
			        document.frmNote.txtAreaNote.value=xmlHttp.responseText;					
			        }
			      }
			  
			url = "noteAction.php";

		    xmlHttp.open("POST", url, true);
			xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			
			param = 'F_CALL=INSERTNEWTEXT';
			param += '&SUBJECT=' + document.frmNote.txtSubject.value;
			param += '&TEXT=' + document.frmNote.txtAreaNote.value;
			
			xmlHttp.send(param); 

			}
			catch(e){
				alert("Failed to save.");
			}
		}
		
	}	

2.) my display code

	function loadSubjectList(){
		try
		{
	
		var xmlHttp = GetXmlHttpObject();
		var url = null;
		var param = null;
		
		   xmlHttp.onreadystatechange=function()
			  {
			  if(xmlHttp.readyState==4)
				{
				document.getElementById('subjectList').innerHTML=xmlHttp.responseText;
				}
			  }
 
		url = "noteAction.php";

		xmlHttp.open("POST", url, true);
		xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
		param = 'F_CALL=GETSUBJECTLIST';
		
		xmlHttp.send(param);
		
		}
		catch(e){
			alert("Failed to load." + e.message);
		}
			
	}	

3. my php code

	if(isset($_POST['F_CALL'])){
	
	logger('F_CALL IS SET for ' . $_POST['F_CALL']);
	
		if($_POST['F_CALL'] == "GETSUBJECTTEXT"){
			
			echo getSubjectText();
			
			logger("get subject's text");		
		
		}else if($_POST['F_CALL'] == "INSERTNEWTEXT"){
		
			insertSubjectText();
			
			logger("insert subject's text");		
		
		}else if($_POST['F_CALL'] == "GETSUBJECTLIST"){
				
			echo getSubjectList();
				
			logger("get subject lists");	
		
		}
	}

4. my output that proves the randomness happening :

[22-11-07 03:39:14] F_CALL IS SET for GETSUBJECTLIST
[22-11-07 03:39:14] F_CALL IS SET for INSERTNEWTEXT
[22-11-07 03:39:14] get subject lists
[22-11-07 03:39:14] insert subject's text
[22-11-07 03:39:21] F_CALL IS SET for GETSUBJECTLIST
[22-11-07 03:39:21] F_CALL IS SET for INSERTNEWTEXT
[22-11-07 03:39:21] insert subject's text
[22-11-07 03:39:21] get subject lists
[22-11-07 03:39:22] F_CALL IS SET for INSERTNEWTEXT
[22-11-07 03:39:22] F_CALL IS SET for GETSUBJECTLIST
[22-11-07 03:39:22] get subject lists
[22-11-07 03:39:22] insert subject's text
[22-11-07 03:39:22] F_CALL IS SET for INSERTNEWTEXT
[22-11-07 03:39:22] F_CALL IS SET for GETSUBJECTLIST
[22-11-07 03:39:23] insert subject's text
[22-11-07 03:39:23] get subject lists
[22-11-07 03:39:33] F_CALL IS SET for INSERTNEWTEXT
[22-11-07 03:39:33] F_CALL IS SET for GETSUBJECTLIST
[22-11-07 03:39:33] insert subject's text
[22-11-07 03:39:33] get subject lists
[22-11-07 03:39:34] F_CALL IS SET for GETSUBJECTLIST
[22-11-07 03:39:34] F_CALL IS SET for INSERTNEWTEXT
[22-11-07 03:39:34] insert subject's text
[22-11-07 03:39:34] get subject lists
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,009
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: ajax refresh??

  #2  
Nov 22nd, 2007
Ajax is asynchronous.

The time taken by the request to be completed and the response to reach the client depends on the time take at the server for processing each of the requests which may vary. A better way would be to call the 'loadSubjectList()' function inside the 'onreadychange' event handler of 'insertNew()' when the response is ready.

Also consider using a lightweight library for handling Ajax functionality since this can make you more productive and help you concentrate on the logic instead of cleaning the mess left by your Ajax code.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

Re: ajax refresh??

  #3  
Nov 22nd, 2007
thanks for pointing that out sos, i didn't realise what i was doing on my readystate on insertnew()
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 9:07 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC