AJAX problem

B

Bruce A. Julseth

I have the following code snippet:

function LoadLists() {
makerequest('SelectMonth.htm','Mo');
makerequest('SelectDay.htm','Da');
makerequest('SelectYear.htm','Yr');
}

//-->
</script>

<body onload="LoadLists()">
<form name="form1" id="form1" method="post" action="">
<label for="select">Year</label>
<div id='Yr'> </div>
<label for="select">Month</label>
<div id='Mo'> </div>
<label for="select">Day</label>
<div id='Da'> </div>
</form>
</body>

Only the last call in LoadList() is in the web page when it is displayed. I
can make one of the other calls the last call and it will be displayed. The
Select???.htm's contain <select><option> html.
 
G

Gregor Kofler

Bruce A. Julseth meinte:
I have the following code snippet:

It /definitely/ is a snippet.
function LoadLists() {
makerequest('SelectMonth.htm','Mo');
makerequest('SelectDay.htm','Da');
makerequest('SelectYear.htm','Yr');
}

//-->
</script>

<body onload="LoadLists()">
<form name="form1" id="form1" method="post" action="">
<label for="select">Year</label>
<div id='Yr'> </div>
<label for="select">Month</label>
<div id='Mo'> </div>
<label for="select">Day</label>
<div id='Da'> </div>
</form>
</body>

Only the last call in LoadList() is in the web page when it is displayed. I
can make one of the other calls the last call and it will be displayed. The
Select???.htm's contain <select><option> html.

Im pretty sure the error is in line 7.

HTH, Gregor
 
B

Bruce A. Julseth

Gregor Kofler said:
Bruce A. Julseth meinte:

It /definitely/ is a snippet.


Im pretty sure the error is in line 7.

HTH, Gregor

Thanks for the response. Line 7 is the standard comment end for JavaScript.
On your suggestion, I took the comment out. Didn't change anything? Here is
the makerequest function. It's right out of the book, "Beginning Ajax with
PHP." It seems to work in all the book examples.

function makerequest(serverPage, objID) {

var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
 
J

Jeremy J Starcher

[ Code Snipped ]
Thanks for the response. Line 7 is the standard comment end for
JavaScript. On your suggestion, I took the comment out. Didn't change
anything? Here is the makerequest function. It's right out of the book,
"Beginning Ajax with PHP." It seems to work in all the book examples.

function makerequest(serverPage, objID) {

var obj = document.getElementById(objID); xmlhttp.open("GET",
serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}

Gregor's comment was a more-or-less standard response when one doesn't
show enough code to be useful. In this case, your original snipping did
not contain the section that was in error.

(And using <!-- //--> with Javascript went obsolete about 10 years ago or
so. Its only standard by people who don't know why they were used and
why not to. They part of my list on how to recognize bad Javascript.)

I would recommend and entirely different approach anyways. Since you are
-already- using php to generate page content, generate the ENTIRE page
using PHP and serve it. Javascript should [almost] always be an
ENHANCEMENT to a page, not a requirement.
makerequest('SelectMonth.htm','Mo');

Secondly, if you are only loading html fragments, as opposed to an entire
page, I would not use the extension '.htm' Personally, I'd use '.txt'.

You still have not shown enough context to allow anyone to answer your
question. Posting a link would be a Good Idea(tm).
 
T

Thomas 'PointedEars' Lahn

The form might not need a name or an ID.
<label for="select">Year</label>
<div id='Yr'> </div>
<label for="select">Month</label>
<div id='Mo'> </div>
<label for="select">Day</label>
<div id='Da'> </div>
</form>
</body>

Only the last call in LoadList() is in the web page when it is displayed.
I can make one of the other calls the last call and it will be displayed.
The Select???.htm's contain <select><option> html.

Im pretty sure the error is in line 7.
[...]

Please trim your quotes to the necessary minimum.
[...] Line 7 is the standard comment end for JavaScript.

Nonsense. It is a CDC (Comment Declaration Close) delimiter hidden from the
JS engine as a single-line comment; a practice that, from the standards
perspective, has always been obsolete (HTML 3.2 introduced the `script'
element, a UA is FUBAR if it renders its content).

The point I think Georg was trying to make, though, is that your script code
is syntactically valid, and without knowing about makerequest() there is no
telling where it might otherwise go wrong.
On your suggestion, I took the comment out. Didn't change anything?

It might have, especially with XHTML, which would then not be well-formed.
Here is the makerequest function. It's right out of the book,
"Beginning Ajax with PHP." It seems to work in all the book examples.

function makerequest(serverPage, objID) {

var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage);

What is `xmlhttp'?
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

Is this running via HTTP(S) on a Web server? Otherwise status can be 0.
obj.innerHTML = xmlhttp.responseText;

Possibility: The space-containing `div' elements are considered empty and
thus can be non-existent at the mercy of the DOM implementation.

That said, with (server-side) PHP you probably don't need all that but you
can (and should) include the form controls in the first place.

<http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork>
<http://www.jibbering.com/faq/#debugging>


PointedEars
 
G

Gregor Kofler

Bruce A. Julseth meinte:
On your suggestion, I took the comment out. Didn't change anything? Here is
the makerequest function. It's right out of the book, "Beginning Ajax with
PHP." It seems to work in all the book examples.

I hope you realize that this code is only for toying around, nothing
that can be used on a "serious" webpage.
function makerequest(serverPage, objID) {

var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}

And where is xmlhttp defined? With every call of makerequest() xmlhttp
gets freshly initialized. Does this make clearer, where the erroneous
behaviour comes from?

Gregor
 
T

Thomas 'PointedEars' Lahn

Thomas said:
The point I think Georg was trying to make, though, [...]

*Gregor*, sorry. Somehow I always manage to confuse you two.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Gregor said:
Bruce A. Julseth meinte:
function makerequest(serverPage, objID) {
[...]
xmlhttp.open("GET", serverPage);
[...]
xmlhttp.send(null);
}

And where is xmlhttp defined? With every call of makerequest() xmlhttp
gets freshly initialized. Does this make clearer, where the erroneous
behaviour comes from?

It does (d'oh!). JFTR: One MUST NOT call x.open() or x.send() while a
request with the object referred to by `x' is in progress. At least the
Gecko source code tells the whole story.


PointedEars
 
B

Bruce A. Julseth

Jeremy J Starcher said:
[ Code Snipped ]
Thanks for the response. Line 7 is the standard comment end for
JavaScript. On your suggestion, I took the comment out. Didn't change
anything? Here is the makerequest function. It's right out of the book,
"Beginning Ajax with PHP." It seems to work in all the book examples.

function makerequest(serverPage, objID) {

var obj = document.getElementById(objID); xmlhttp.open("GET",
serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}

Gregor's comment was a more-or-less standard response when one doesn't
show enough code to be useful. In this case, your original snipping did
not contain the section that was in error.

(And using <!-- //--> with Javascript went obsolete about 10 years ago or
so. Its only standard by people who don't know why they were used and
why not to. They part of my list on how to recognize bad Javascript.)

I would recommend and entirely different approach anyways. Since you are
-already- using php to generate page content, generate the ENTIRE page
using PHP and serve it. Javascript should [almost] always be an
ENHANCEMENT to a page, not a requirement.
makerequest('SelectMonth.htm','Mo');

Secondly, if you are only loading html fragments, as opposed to an entire
page, I would not use the extension '.htm' Personally, I'd use '.txt'.

You still have not shown enough context to allow anyone to answer your
question. Posting a link would be a Good Idea(tm).

I admit that I am a novice at JavaScript, but I still see the comment
characters in books that have 2007 and 2008 copywrite dates. So, I have
continued to use them.

Doing the entire page in PHP would solve every problem but one. AJAX seems
to me to the best way to solve this one problem, namely I want to CHANGE the
DAY Select/Option list to agree with the number of days in a month. When a
client chooses February, I only want 28/29 days displayed, January, 31 days,
etc.

I'm DON"T know how to do this. Suggestion?
 
B

Bruce A. Julseth

Thomas 'PointedEars' Lahn said:
The form might not need a name or an ID.
<label for="select">Year</label>
<div id='Yr'> </div>
<label for="select">Month</label>
<div id='Mo'> </div>
<label for="select">Day</label>
<div id='Da'> </div>
</form>
</body>

Only the last call in LoadList() is in the web page when it is
displayed.
I can make one of the other calls the last call and it will be
displayed.
The Select???.htm's contain <select><option> html.

Im pretty sure the error is in line 7.
[...]

Please trim your quotes to the necessary minimum.
[...] Line 7 is the standard comment end for JavaScript.

Nonsense. It is a CDC (Comment Declaration Close) delimiter hidden from
the
JS engine as a single-line comment; a practice that, from the standards
perspective, has always been obsolete (HTML 3.2 introduced the `script'
element, a UA is FUBAR if it renders its content).

The point I think Georg was trying to make, though, is that your script
code
is syntactically valid, and without knowing about makerequest() there is
no
telling where it might otherwise go wrong.
On your suggestion, I took the comment out. Didn't change anything?

It might have, especially with XHTML, which would then not be well-formed.
Here is the makerequest function. It's right out of the book,
"Beginning Ajax with PHP." It seems to work in all the book examples.

function makerequest(serverPage, objID) {

var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage);

What is `xmlhttp'?
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

Is this running via HTTP(S) on a Web server? Otherwise status can be 0.
obj.innerHTML = xmlhttp.responseText;

Possibility: The space-containing `div' elements are considered empty and
thus can be non-existent at the mercy of the DOM implementation.

That said, with (server-side) PHP you probably don't need all that but you
can (and should) include the form controls in the first place.

<http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork>
<http://www.jibbering.com/faq/#debugging>


PointedEars

Okay... My snippet was to "LITTLE." Here is all the Javascript and HTML for
the page...

<script type="text/javascript">
//Create a boolean variable to check for a valid Internet Explorer
instance.
var xmlhttp = false;

//Check if we are using IE.
try {
//If the Javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
// alert("You are using Microsoft IE");
} catch (e) {
//If not, then use the older active x object.
try {
//If we are using MS.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
// alert("You are using Microsoft IE");
} catch (E) {
//Else we must be using a non-IE browser.
xmlhttp = false;
}
}

//If we are using a non-IE browser, create a javascript instance of the
object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
// alert("You are not using Microsoft IE");
}


/*
var xmlhttp;

//If, the activexobject is available, we must be using IE.
if (window.ActiveXObject){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
//Else, we can use the native Javascript handler.
xmlhttp = new XMLHttpRequest();
}
*/

function makerequest(serverPage, objID) {

var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}

function LoadLists() {
makerequest('SelectDay.htm','Da');
makerequest('SelectYear.htm','Yr');
makerequest('SelectMonth.htm','Mo');
}

</script>
</head>
<body onload="LoadLists()">
<form name="form1" id="form1" method="post" action="">
<label for="select1">Year<div id='Yr'></div></label>
<label for="select2">Month<div id='Mo'></div></label>
<label for="select3">Day<div id='Da'></div></label>
</form>
</body>
 
B

Bruce A. Julseth

Gregor Kofler said:
Bruce A. Julseth meinte:


I hope you realize that this code is only for toying around, nothing
that can be used on a "serious" webpage.

Oh, boy.. What's wrong with code? I'd GREATLY appreciate you comments on how
I need to improve it. I'm espcially interested in the function makerequest.
I know my HTML Web is "play" code. I'm trying to find out how load and
modify my Select/Option code dynamically. Thanks...
And where is xmlhttp defined? With every call of makerequest() xmlhttp
gets freshly initialized. Does this make clearer, where the erroneous
behaviour comes from?

I loaded all the JavaScript in another response. Appreciate you looking at
 
T

Thomas 'PointedEars' Lahn

Bruce said:
I admit that I am a novice at JavaScript, but I still see the comment
characters in books that have 2007 and 2008 copywrite dates. So, I have
continued to use them.

In fact, I have yet to see a book about the use of ECMAScript
implementations that can be recommended to novices.
Doing the entire page in PHP would solve every problem but one.

You are mistaken.
AJAX seems to me to the best way to solve this one problem, namely I want
to CHANGE the DAY Select/Option list to agree with the number of days in
a month. When a client chooses February, I only want 28/29 days displayed,
January, 31 days, etc.

You can (and SHOULD) generate the controls with server-side PHP, and update
them with client-side scripting only when necessary. You really don't need
XHR for this; ECMAScript implementations provide a Date object that you can
use without further request.
I'm DON"T know how to do this. Suggestion?

STFW and this newsgroup for the `options' NodeList. And trim your quotes.

<http://jibbering.com/faq/>


PointedEars
 
T

Thomas 'PointedEars' Lahn

Bruce said:

Please don't post attribution novels.

Bruce said:
:
Bruce A. Julseth meinte:
function LoadLists() {
 makerequest('SelectMonth.htm','Mo');
 makerequest('SelectDay.htm','Da');
 makerequest('SelectYear.htm','Yr');
}

//-->
 </script>
[...]

Im pretty sure the error is in line 7.
[...]

Please trim your quotes to the necessary minimum.

(Which part of that did you not understand?)
[...]
The point I think Georg was trying to make, though, is that your script
code is syntactically valid, and without knowing about makerequest()
there is no telling where it might otherwise go wrong.

[...]
function makerequest(serverPage, objID) {

var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage);

What is `xmlhttp'?
[...]

Okay... My snippet was to "LITTLE."

(Please don't SHOUT here.)
Here is all the Javascript and HTML for the page...

Thanks, although it turned out already that it was not longer necessary for
analyzing the problem. It is useful as a bad example, though.
<script type="text/javascript">
//Create a boolean variable to check for a valid Internet Explorer
instance.

The comment does not make any sense at all. Was this your idea or is it
from the book(s)?
var xmlhttp = false;

var xmlhttp = null;

makes more sense, as *an object reference* will be assigned later.
//Check if we are using IE.
Nonsense.

try {
//If the Javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

The "Javascript version" (whatever the author thought that would be) has
nothing to do with the MSXML version. The former is the programming
language, the latter is an API that the UA's runtime environment provides
which can be used with that language (and other languages).

Get yourself informed, or reuse the book(s) (for a nice camp fire, paper
flyers ... you get the idea?)

// alert("You are using Microsoft IE");

A distinct possibility with MSXML support being present, certainly not a
necessity.
} catch (e) {
//If not, then use the older active x object. ^^^^^
try {
//If we are using MS.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

In 99.9% of the cases, this identifier string and test, which will AFAIK
pick the *newest* installed version of MSXML, suffices for XHR.
// alert("You are using Microsoft IE");
Nonsense.

} catch (E) {
//Else we must be using a non-IE browser.
xmlhttp = false;

Again, ActiveX support has nothing to do with IE or not IE.
}
}

//If we are using a non-IE browser, create a javascript instance of the
object.

The comment is nonsense as IE 7 supports XMLHttpRequest(), too. It only
will not execute the block statement below because it also supports
ActiveXObject().
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

Insufficient feature test. That XMLHttpRequest is not `undefined' is not an
indication that it can be called.
xmlhttp = new XMLHttpRequest();

Missing exception handling although that would not add more incompatibility
to the code. (try...catch was not introduced before ECMAScript 3; it
causes the code to not compile with older implementations and in older or
lightweight user agents, and thus must be handled specially).
// alert("You are not using Microsoft IE");

Non sequitur.
}


/*
var xmlhttp;

//If, the activexobject is available, we must be using IE.
if (window.ActiveXObject){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
//Else, we can use the native Javascript handler.
xmlhttp = new XMLHttpRequest();
}
*/

The same nonsense repeated.
function makerequest(serverPage, objID) {
[...]

That's online already.


PointedEars
 
G

Gregor Kofler

Bruce A. Julseth meinte:
Oh, boy.. What's wrong with code? I'd GREATLY appreciate you comments on how
I need to improve it. I'm espcially interested in the function makerequest.

Hey, it's *you* who got the smart book. The code is nothing more than a
quick simple example.

It features
- no feature tests
- no IE support
- no timeout handling
- no cancellation of requests
- etc.

Perhaps others are willing to provide a detailed and robust solution for
you. Though I doubt that. It has been a topic here frequently and there
are (a few good and a lot bad) libraries out there.

Gregor
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
(And using <!-- //--> with Javascript went obsolete about 10 years ago or
so. Its only standard by people who don't know why they were used and
why not to. They part of my list on how to recognize bad Javascript.)

That is an unsafe assumption, because it presumes that the JavaScript is
only going to be read by people, editors, and browsers not much more
than (maybe) a decade old.

It ignores the possibility of the comment being there because the file
containing the JavaScript will be processed, before loading to the Web,
by an elderly but still useful piece of software which was designed to
read HTML (understanding <!-- and -->) but not to read JavaScript.
 
B

Bruce A. Julseth

Gregor Kofler said:
Gregor Kofler meinte:


Oops. I read your full example too late. Thomas has already made his
annotations.

Gregor

Thanks for the response. I still don't know why my "example" doesn't work.
Maybe someone knows of an example somewhere. What I want to do is create 3
drop down list boxes:, Year, Month, Day. On load, I want the Year to be
loaded with the current year and next year. No problem. I know how to do
that. I want to the Month box to loaded with the 12 months. No problem. I
know how to do that. I want the Day box to load, after the Month is selected
with the days in the month selected. I don't know how to do that. I was
expecting to use AJAX to load the Day box with an "onblur" response to the
month selection.

Does anyone know of an example that does what I am trying to do?

Thanks again for the response.
 
G

Gregor Kofler

Bruce A. Julseth meinte:
Thanks for the response. I still don't know why my "example" doesn't work.
Maybe someone knows of an example somewhere. What I want to do is create 3
drop down list boxes:, Year, Month, Day.

Thomas explained it already. You're gonna need 3 *different* XHR objects
and not re-initialize the same one all the time.

Gregor
 
T

Thomas 'PointedEars' Lahn

Dr said:
Jeremy J Starcher posted:

That is an unsafe assumption, because it presumes that the JavaScript is
only going to be read by people, editors, and browsers not much more
than (maybe) a decade old.

Nonsense. HTML 2.0 (as of 1995-11) has been officially declared OBSOLETE
about eight years, six months ago:

It ignores the possibility of the comment being there because the file
containing the JavaScript will be processed, before loading to the Web,
by an elderly but still useful piece of software which was designed to
read HTML (understanding <!-- and -->) but not to read JavaScript.

That's but a lame excuse for still using and supporting software FUBAR. If
that piece of software was really that useful to everyone, one would think
their authors would have taken the time to update it by now. For it is not
that they could not use a working open-source parser instead.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
Wed, 14 Jan 2009 04:53:34, Thomas 'PointedEars' Lahn
Please don't post attribution novels.

Ignore that; Lahn is a control freak with peculiar ideas. The bandwidth
consumed by an informative attribution is negligible; it is much less
than that consumed by the articles requires to inform those who suffer
from Lahn that the fault lies in him and not in what they have posted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top