View Full Version : using AJAX and the XMLhttprequest

11-10-2010, 09:02 PM
hello all,

I am trying to see if the w3schools example works on my site with a collapsible panel. here is what I have:
<script type="text/javascript">
function getXMLcontent() {
if (window.XMLHttpRequest)

xmlhttp=new XMLHttpRequest();
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

document.getElementById('CollapsiblePanel1').style.visibility = 'visible';
xmlhttp.open("GET","showfunction.php?fname=" + document.getElementById('functions').value, true);
alert("showfunction.php?fname=" + document.getElementById('functions').value);
the panel shows up as a thin grey line because no text is being thrown into it from the 'responsetext' property. I don't really know anything about what I'm doing. Just trying to copy and paste what the website example is. is there anything else I should know about what I'm doing, that would at least tell me why I'm not getting the data back out of the server like I should be?

the 'functions' element on the page is a dropdown box, but it is not inside of a form tag. does that make a difference since I am using 'GET'??


Old Pedant
11-10-2010, 09:49 PM
Go read the w3schools stuff again.

You can't get the responseText *BEFORE* the send!!!

Further, if you use true as the last argument to open( ), you *MUST* set up a response *HANDLER*, which you haven't even started to do.

11-10-2010, 09:56 PM
that works fine Pedant. this fits the need:
document.getElementById('CollapsiblePanel1').style.visibility = 'visible';
xmlhttp.open("GET","showfunction.php?fname=" + document.getElementById('functions').value, false);

Old Pedant
11-10-2010, 10:15 PM
Yep. Using false as the last argument means "wait for the response", so then you don't have to have the event handler.

11-11-2010, 12:20 AM
I don't have the knowledge right now to be dealing with event handlers. so yeah, thanks again for the help sir!

one more question if you don't mind: does this request-type AJAX code only work with an actual page? for instance, do I have to use something like showfunction.php?fname=myfunction to get the content out of the db, or can I pull the content out without using a page URL? I don't really understand this concept.

Can you offer an explanation or two?

Old Pedant
11-11-2010, 01:31 AM
You *MUST* pass a URL.

What you are *ACTUALLY* doing is making exactly the same kind of HTTP Request that a browser makes when you ask the browser to load such a page.

In fact, try that yourself: In the browser's address bar, type in


and look what comes back from the server!

*THAT* is 100% identical to what comes back from the server to the responseText property when you use the AJAX call.

So you *can* read ANY page (php, html, whatever) off of your own site using AJAX coding this way.

The whole point is to create a special page that returns just exactly what your AJAX code wants/needs for its purpose.

There's really nothing magic: It's just that XMLHTTP object acting as a sort of mini-browser to go out and get the URL you asked for.

11-11-2010, 07:22 AM

is this the only technique that is available by the AJAX language?

Old Pedant
11-12-2010, 12:10 AM
Sorry...don't understand the question.

Oh, and AJAX isn't a language. It is, itself, just a "technique".

Technically, what you used wasn't AJAX.

AJAX is just an acronym: Asynchronous Javascript And Xml.

What you did was use a synchronous call. So I guess we could name it SJAX.

Except what you returned to yourself wasn't XML. It was just text. So maybe it should be called SJAT?

But everybody tosses async and sync and xml and text all together as AJAX. Just to confuse the issue? Or maybe oversimplify it. Whatever.

11-12-2010, 06:38 AM
the question is: what did I learn how to do on w3schools? use a call? is that kind of "process" called "AJAX"? as in, is that what it's all about? if so, then the answer is yes. and if so, AJAX is really nothing, just a technology developed by another developer.

Old Pedant
11-12-2010, 08:42 PM
AJAX is really nothing, just a technology developed by another developer.

You got it.

Long before the XMLHTTP object appeared in browsers, and even before <iframe>s, I used to accomplish this same thing using <frame>s.

I would have a main visible frame that would ask for a particular URL to be put into another completely hidden frame. That URL would return a page that had no important content *other* than JavaScript, and the JavaScript would be used to modify the contents of the visible frame.

Then I got a little more clever: I created a "callback" function in the main frame with a standard name. And then the code loaded into the hidden frame would, instead of directly modifying the main frame, call that callback function when it got loaded, passing a reference to a set of returned data (typically a JavaScript array of objects).

Please tell me how this differs, in concept, from AJAX used with JSON today?

Actually, I still often use this technique, today, when I want to send <form method=post> data to the server. It is *SO* much easier to just use
<form target="iframeName" method="post" action="processFormAndRespond.xxx">
than it is to build up an AJAX request. So why bother? I accomplish the same thing with less work. I now use <iframe>s, but other than that my technique hasn't changed much since 1998.

But don't say that a technology like AJAX is "nothing". By making it somewhat of a standard, companies such as Google can develop entire products that can be delivered via the web.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum