Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > AJAX problem

Reply
Thread Tools

AJAX problem

 
 
Bruce A. Julseth
Guest
Posts: n/a
 
      01-13-2009
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.


 
Reply With Quote
 
 
 
 
Gregor Kofler
Guest
Posts: n/a
 
      01-13-2009
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
 
Reply With Quote
 
 
 
 
Bruce A. Julseth
Guest
Posts: n/a
 
      01-13-2009

"Gregor Kofler" <(E-Mail Removed)> wrote in message
news:Fn8bl.138$(E-Mail Removed)...
> 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


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



 
Reply With Quote
 
Jeremy J Starcher
Guest
Posts: n/a
 
      01-13-2009
On Tue, 13 Jan 2009 17:31:33 -0500, Bruce A. Julseth wrote:

> "Gregor Kofler" <(E-Mail Removed)> wrote in message
> news:Fn8bl.138$(E-Mail Removed)...
>> Bruce A. Julseth meinte:
>>> I have the following code snippet:

>>
>> It /definitely/ is a snippet.
>>


[ Code Snipped ]

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


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).
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-13-2009
Bruce A. Julseth wrote:
> "Gregor Kofler" wrote:
>> Bruce A. Julseth meinte:
>>> 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="">


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
 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      01-13-2009
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-13-2009
Thomas 'PointedEars' Lahn wrote:
> The point I think Georg was trying to make, though, [...]


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


PointedEars
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-13-2009
Gregor Kofler wrote:
> 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
 
Reply With Quote
 
Bruce A. Julseth
Guest
Posts: n/a
 
      01-14-2009

"Jeremy J Starcher" <(E-Mail Removed)> wrote in message
news:w69bl.11284$(E-Mail Removed)...
> On Tue, 13 Jan 2009 17:31:33 -0500, Bruce A. Julseth wrote:
>
>> "Gregor Kofler" <(E-Mail Removed)> wrote in message
>> news:Fn8bl.138$(E-Mail Removed)...
>>> Bruce A. Julseth meinte:
>>>> I have the following code snippet:
>>>
>>> It /definitely/ is a snippet.
>>>

>
> [ Code Snipped ]
>
>>>
>>> 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);
>> }

>
> 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?


 
Reply With Quote
 
Bruce A. Julseth
Guest
Posts: n/a
 
      01-14-2009

"Thomas 'PointedEars' Lahn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bruce A. Julseth wrote:
>> "Gregor Kofler" wrote:
>>> Bruce A. Julseth meinte:
>>>> 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="">

>
> 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>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An Ajax Dictionary... My first AJAX project... GreatDomainz@gmail.com HTML 20 03-31-2008 01:24 PM
Deploying ASP.NET AJAX-Enabled Web site to host without AJAX insta =?Utf-8?B?QnJpYW4gRWR3YXJkcw==?= ASP .Net 2 02-21-2007 10:22 PM
AJAX IDE and AJAX TOOL--The Release of JoyiStar AJAX WebShop 3 Beta minnie Java 1 12-13-2006 06:29 AM
Ajax installed.. where are Ajax controls like popup in my VS toolbox ?? jobs ASP .Net 0 11-06-2006 01:23 AM
beginner AJAX questions (AJAX.net) darrel ASP .Net 5 04-28-2006 06:42 PM



Advertisments