473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ajax refreshing problem

7
hi guys,

I'm new to ajax and i'm just following the examples from tizag and w3schools. Apparently, an application i'm trying to build is showing very peculiar behaviour.

I have a <span> section where it'll actually list out all my subject titles dynamically after a record has been inserted. However, it doesn't always list out all items with the latest added subject.

BUT

if i put an alert() before it loads the list, i won't have this problem! What's going on? is alert triggering something ? a state on ajax?

I'm using oracle 9, IE 7 and Firefox 2.

below is my ajax code that actually works.

[PHP]

function loadSubjectList(){
try
{

var xmlHttp = GetXmlHttpObject();
var url = null;
var param = null;

xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById('subjectList').innerHTML=x mlHttp.responseText;
}
}

alert('Ahh!'); //need this to make below refresh 100%

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);
}

}

[/PHP]
Nov 22 '07 #1
7 1332
adrive
7
apparently after a deeper research, the problem seems to be even weirder. The sequence of ajax execution is random!

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

------------------------------------------------------------------------
1.) my insert code :

[PHP]
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.respons eText;
}
}

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.");
}
}

}
[/PHP]

2. my display code :

[PHP]

function loadSubjectList(){
try
{

var xmlHttp = GetXmlHttpObject();
var url = null;
var param = null;

xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById('subjectList').innerHTML=x mlHttp.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);
}

}
[/PHP]

3. my php code :

[PHP]

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");

}
}

[/PHP]

4. my output on a logger :

[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
Nov 22 '07 #2
adrive
7
anyone? or perhaps this might be due to multi request calls?
Nov 22 '07 #3
acoder
16,027 Expert Mod 8TB
If you want this in sequence, you will have to wait until the first request is complete before making the second request.
Nov 22 '07 #4
adrive
7
If you want this in sequence, you will have to wait until the first request is complete before making the second request.
hi, how do i check if the first request was completed?
Nov 23 '07 #5
acoder
16,027 Expert Mod 8TB
hi, how do i check if the first request was completed?
When readyState is 4. You could make the second request in the onreadystatechange-called function when the first request's readyState is 4.
Nov 23 '07 #6
adrive
7
thanks acoder, it now works :)
Nov 24 '07 #7
acoder
16,027 Expert Mod 8TB
You're welcome. Glad you got it working.
Nov 26 '07 #8

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

Similar topics

3
by: user | last post by:
Hi, let's say a page loads on a browser. Without refreshing the page, multiple textboxes within the page gets real time data @ a few seconds interval. what is the logic behind to achieve...
1
by: santhubaba | last post by:
Hi All, I am facing a problem with ajax in dotnet(c#) page My problem is as follows: There is a grid with many buttons , one of the buttons is "Upload" ,if i click on that upload then...
7
by: =?Utf-8?B?Tmlrb2xheSBFdnNlZXY=?= | last post by:
Hi! I know this topic has been discussed a long way, but I haven't found any apparent solution (maybe I shouldn't be looking for a one :)) I have a very simple application with one page and with...
10
by: Piotr Nowak | last post by:
Hi, Say i have a server process which listens for some changes in database. When a change occurs i want to refresh my page in browser by notyfinig it. I do not want to refresh my page i.e....
25
by: Piotr Nowak | last post by:
Hi, Say i have a server process which listens for some changes in database. When a change occurs i want to refresh my page in browser by notyfinig it. I do not want to refresh my page i.e....
3
by: burrashiva | last post by:
:( ::) Hello there, I recently started Web Designing with PHP, Javascript and AJAX. I am facing a problem which I will try to explain: If I am successful in conveying the problem to you...
1
by: bizt | last post by:
Hi, Im currently looking to move into using JSON for AJAX instead of returning from the server a string like the following: 12345{This is a text string{true I prefer objects because I dont...
1
by: prarthanaraut | last post by:
Hi, I am getting Browser problerm while using AJAX in classic ASP code. I want to execute some asp code when user clicks on checkboxes on one page without refreshing the page. I have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
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,...

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.