473,808 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XMLHttpRequest question

I have an online store that a customer needs customized by having notes for
each product. I have added a javascript function that sends a session id,
unique product id and some user-defined notes to a script using
XMLHttpRequest. The values get there fine and get stored in a database.

Upon checkout (on the checkout page) I now want to pull all of the data back
from the database and display it. I don't need to display the notes in
order to get them to send, I just want to customer to see their notes so
they don't think they are lost.

So, in my checkout page I have a loop that created divs like the following
<div id="notes<numbe r>"></div>
where <number> is an incremental number starting from 1.

Then, inside the div I have a 1x1 transparent image with an onload function
call.

This onload function call looks something like this:
onload="getNote s(<number>,<ses sion_id>, <product_id>)
where <number> is the same incremental number from above and <session_id>
and <product_id> are values available inside my loop.

The function getNotes uses XMLHttpConnect to connect to another script and
using the session_id and product_id it retrieves the notes and should
populate the div with the notes.

My problem is this...

The getNotes function get called, then the "onreadystatech ange" function
gets called but only the code outside of the check for the "readyState " and
"status" is executed.
The stuff inside of
if ((xmlhttp.ready State == 4) && (xmlhttp.status == 200)) { }
never gets called.

Is this because the function getNotes is getting called too quickly after
the first call? If this was the case, I would expect the last one to
work... since it would be the last call.
Is there some way to fix this? Do I need to call the onreadystatecha nge
function with a setTimeout so that it loops, checking the readyState and
status?

Thanks for any and all help.

Sorry if I did not explain fully.

Nov 3 '05 #1
5 1630
Sean,

You are, indeed, checking the readyState before the server has a chance to
reply. You'd probably be better off using xmlhttp.readyst atechange to watch
for that reply. Here's some code that should help:

xmlhttp.onready statechange = function() {
if ( xmlhttp.readySt ate == 4 ) {
if ( xmlhttp.status == 200 ) {
doSomeMagic();
} else {
didntWorkOut();
}
}
}
xmlhttp.onready statechange=fun ction() {
if (xmlhttp.readyS tate==4) {
alert(xmlhttp.r esponseText)
}
}
"Sean Berry" <se**@buildingo nline.com> wrote in message
news:1Muaf.4771 $UF4.2202@fed1r ead02...
I have an online store that a customer needs customized by having notes for
each product. I have added a javascript function that sends a session id,
unique product id and some user-defined notes to a script using
XMLHttpRequest . The values get there fine and get stored in a database.

Upon checkout (on the checkout page) I now want to pull all of the data
back from the database and display it. I don't need to display the notes
in order to get them to send, I just want to customer to see their notes
so they don't think they are lost.

So, in my checkout page I have a loop that created divs like the following
<div id="notes<numbe r>"></div>
where <number> is an incremental number starting from 1.

Then, inside the div I have a 1x1 transparent image with an onload
function call.

This onload function call looks something like this:
onload="getNote s(<number>,<ses sion_id>, <product_id>)
where <number> is the same incremental number from above and <session_id>
and <product_id> are values available inside my loop.

The function getNotes uses XMLHttpConnect to connect to another script and
using the session_id and product_id it retrieves the notes and should
populate the div with the notes.

My problem is this...

The getNotes function get called, then the "onreadystatech ange" function
gets called but only the code outside of the check for the "readyState "
and "status" is executed.
The stuff inside of
if ((xmlhttp.ready State == 4) && (xmlhttp.status == 200)) { }
never gets called.

Is this because the function getNotes is getting called too quickly after
the first call? If this was the case, I would expect the last one to
work... since it would be the last call.
Is there some way to fix this? Do I need to call the onreadystatecha nge
function with a setTimeout so that it loops, checking the readyState and
status?

Thanks for any and all help.

Sorry if I did not explain fully.

Nov 3 '05 #2
Thanks Mike.

I actually figured out the first of my problems. I was doing the following
xmlhttp.onready statechange = myfunction(para m1, param2, param3);

I changed that to
xmlhttp.onready statechange = function() {myfunction(par am1, param2,
param3)};

Now it works as I expected in my first post... it only loads the last
"notes"

So, how can I ensure that the value has been received before going on to the
next one?
Thanks for any ideas...


"Mike P" <mi************ ************@no spam.com> wrote in message
news:Or******** ************@co mcast.com...
Sean,

You are, indeed, checking the readyState before the server has a chance to
reply. You'd probably be better off using xmlhttp.readyst atechange to
watch for that reply. Here's some code that should help:

xmlhttp.onready statechange = function() {
if ( xmlhttp.readySt ate == 4 ) {
if ( xmlhttp.status == 200 ) {
doSomeMagic();
} else {
didntWorkOut();
}
}
}
xmlhttp.onready statechange=fun ction() {
if (xmlhttp.readyS tate==4) {
alert(xmlhttp.r esponseText)
}
}
"Sean Berry" <se**@buildingo nline.com> wrote in message
news:1Muaf.4771 $UF4.2202@fed1r ead02...
I have an online store that a customer needs customized by having notes
for each product. I have added a javascript function that sends a session
id, unique product id and some user-defined notes to a script using
XMLHttpReques t. The values get there fine and get stored in a database.

Upon checkout (on the checkout page) I now want to pull all of the data
back from the database and display it. I don't need to display the notes
in order to get them to send, I just want to customer to see their notes
so they don't think they are lost.

So, in my checkout page I have a loop that created divs like the
following
<div id="notes<numbe r>"></div>
where <number> is an incremental number starting from 1.

Then, inside the div I have a 1x1 transparent image with an onload
function call.

This onload function call looks something like this:
onload="getNote s(<number>,<ses sion_id>, <product_id>)
where <number> is the same incremental number from above and <session_id>
and <product_id> are values available inside my loop.

The function getNotes uses XMLHttpConnect to connect to another script
and using the session_id and product_id it retrieves the notes and should
populate the div with the notes.

My problem is this...

The getNotes function get called, then the "onreadystatech ange" function
gets called but only the code outside of the check for the "readyState "
and "status" is executed.
The stuff inside of
if ((xmlhttp.ready State == 4) && (xmlhttp.status == 200)) { }
never gets called.

Is this because the function getNotes is getting called too quickly after
the first call? If this was the case, I would expect the last one to
work... since it would be the last call.
Is there some way to fix this? Do I need to call the onreadystatecha nge
function with a setTimeout so that it loops, checking the readyState and
status?

Thanks for any and all help.

Sorry if I did not explain fully.


Nov 3 '05 #3
"Sean Berry" <se**@buildingo nline.com> wrote in message
news:gfvaf.4772 $UF4.3231@fed1r ead02...
Now it works as I expected in my first post... it only loads the last
"notes"
So, how can I ensure that the value has been received before going on to >
the next one?


Sean,

I'm not sure I completely follow, but if you're asking how to get to param2,
param3, etc. I would assume that's more a function of what you server's
code is sending. Meaning, it depends on the structure of the XML you're
getting back, and how you're handling it. You may want to treat this as an
object (ala, param["note1"] etc.) or as an array (e.g, param[1], param[2],
etc.).

How is the server structuring the XML? How are you processing it when you
get it back?

Mike
Nov 3 '05 #4


"Mike P" <mi************ ************@no spam.com> wrote in message
news:gp******** ************@co mcast.com...
"Sean Berry" <se**@buildingo nline.com> wrote in message
news:gfvaf.4772 $UF4.3231@fed1r ead02...
Now it works as I expected in my first post... it only loads the last
"notes"
So, how can I ensure that the value has been received before going on to
> the next one?


Sean,

I'm not sure I completely follow, but if you're asking how to get to
param2, param3, etc. I would assume that's more a function of what you
server's code is sending. Meaning, it depends on the structure of the XML
you're getting back, and how you're handling it. You may want to treat
this as an object (ala, param["note1"] etc.) or as an array (e.g,
param[1], param[2], etc.).

How is the server structuring the XML? How are you processing it when you
get it back?

Mike


Mike,

Thanks for the reply.

I figured out the answer to my problem... actually quite simple.
----Problem----
I have a loop that runs and for each item it creates an <img> tag that has
an oncall function. The oncall function connects to a script via
XMLHttpRequest. The problem was that the function was being called the
first time from the first image during the first run in the loop. Then the
function would be called again by the second image created during the second
loop through. So on and so on for each item. Because there was some
latency, once the second call to the function occured it would stop the
first onreadystatecha nge() and start trying to run it with the for the
second set of parameters. Then, if there was a third, it would stop the
second. So on and so forth. Once it called the function for the last time
(last item, so last run through the loop) it would complete and the
responseText could be used and every thing was super for the last one... all
the others never worked.

----Solution----
In my retrieveNotes() function I have something like the following:

var processing = 0;
function retrieveNotes(v alue) {
if (processing == 0) {
processing = 1;
create xmlhttp object
set up the connect string
set up onreadystatecha nge = doSomething;
do connection, etc
} else {
setTimeout("ret rieveNotes('" + value + "')", 1000);
}
}

then in my onreadystatecha nge function

function doSomething() {
if (xmlhttp.readyS tate == 4 && smlhttp.status == 200) {
do stuff with responseText;
processing = 0;
}
}

So now the retrieveNotes function will either process the request if it is
done processing the request before it goes on to the next one.

If the process is not complete, a setTimeout loop is started and called
until the prior process is complete.

Thanks for the help anyway.

Sean

Nov 4 '05 #5

"Sean Berry" <se**@buildingo nline.com> wrote in message
news:1syaf.4805 $UF4.3703@fed1r ead02...
<snip> ----Solution----
In my retrieveNotes() function I have something like the following:

var processing = 0;
function retrieveNotes(v alue) {
if (processing == 0) {
processing = 1;
create xmlhttp object
set up the connect string
set up onreadystatecha nge = doSomething;
do connection, etc
} else {
setTimeout("ret rieveNotes('" + value + "')", 1000);
}
}

then in my onreadystatecha nge function

function doSomething() {
if (xmlhttp.readyS tate == 4 && smlhttp.status == 200) {
do stuff with responseText;
processing = 0;
}
}

So now the retrieveNotes function will either process the request if it is
done processing the request before it goes on to the next one.

If the process is not complete, a setTimeout loop is started and called
until the prior process is complete.

Thanks for the help anyway.

Sean


Glad I could be of help. I do wonder, though, why you're not just
retrieving an array of notes with a single request, then looping through the
array to display each note. That would elminate the need for your
"processing " flag. Also, is it possible that your users may have more or
less than three notes?

Mike
Nov 4 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2485
by: Elizabeth Harmon | last post by:
Good Evening, I am new to the XMLHTTPRequest Object and it's use but i am doing pretty good with it since it's realtively simple? I am having one problem in using a Get method. I am requesting an aspx file that contains some controls, intersoft Web Combo to be exact. This control is a drop downlist control that renders it's contents in an XML file structure. When i do a XMLHTTPRequest("GET",URL,true), the response text is sent back...
20
2698
by: chris.schwalm | last post by:
This is part II of this <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/cd54951e0ea277de/639c67f2f7cadb1f?tvc=2&q=Simple+HTML+read%2Fwrite+question#639c67f2f7cadb1f"> post</a>. I am creating a java script that bascially reads a webpage - forwards it to an external program/parser/servlet - then overwrites the webpage with a slightly modify version. Basically I have two questions: (1) Do I need to set my...
1
2195
by: 4levels | last post by:
Dear Folks, I stumbled upon a strange behaviour of the XMLHttpRequest.. Maybe I'm just not well informed enough about its possibilities, so could someone please confirm my question? When I put plain javscript in a file that is read-in through a XMLHttpRequest-object, it's like it is totally ignored. Eg. I have the file ajax_include.html with in it's body the following lines <script type="text/javascript" language="javascript">
7
3642
by: pamelafluente | last post by:
The precious input given by Laurent, Martin, Benjamin about XMLHttpRequest in Javascript, has made me think that perhaps I could improve what I am currently doing by using Ajax. Let's make it simple and schematic, to see if there is a simple Ajax answer to this. A. I have an HTML page which has some pure html/css code representing a GRID of cell. The page may also contain other objects (images, etc). B. On the server I have a windows...
1
2269
by: weston | last post by:
So, the following apparently works as a method of grafting a method onto an object (using the squarefree JS shell): obj = { x: 1, inc: null } result obj.inc = function () { this.x++ } result function () { this.x++; } obj.inc() obj.x result 2
1
7710
by: vocalise | last post by:
The title probably isn't very clear, but I haven't been able to find this problem (or I must be having problems figuring out which search strings to use) so I apologize if this has been addressed before, and I apologize if this is the wrong forum since I'm trying to use the XMLHTTPRequest with Javascript and PHP and I couldn't figure out which category my problem belonged to. I am completely new to AJAX and rusty with Javascript. I created a...
5
2412
by: Peter Michaux | last post by:
Hi, The FAQ correctly says the following: "Mozilla (NN6.2+, Firefox, Ice Weasle etc), Opera 7.6+, Safari1.2+, the Windows version of IE versions 5+, and some other browsers provide the XML HTTP Request object." In my haze of testing yesterday it seems that NN6.1 provides an non-functional XMLHttpRequest object and NN6.2 XMLHttpRequest object
1
4036
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest technology is implemented on more sites now than ever. Compatibility is no longer an issue (IE, Mozilla and Opera all support it), and the benefits to using it are amazing. There are too many PHP programmers avoiding any
5
15513
by: HugeBob | last post by:
Hi All, I've got a question about Asynchronous vs Synchronous mode with the XMLHttpRequest object. What are the ramifications of using one mode vs the other? If the script uses Asynchronous mode, it sounds as if a thread retrieves the data from the supplied URL and the JS function that called the open() and send() methods continues on. Where as using Synchronous mode the method that called open() and send() waits until the data from...
25
1368
by: q-rious | last post by:
Hello All, I have a question regarding XMLHttpRequest. I have the following code (as part of a much larger file): function loadXMLDoc(url) { // branch for native XMLHttpRequest object if (window.XMLHttpRequest) { req = new XMLHttpRequest();
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9196
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
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 we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.