If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Ajax call requests not working in all browsers

I've been using a rather simple Ajax call to make forms a little more streamlined and responsive, but I've had some people say it doesn't work in their browser - specifically Chrome, even though I am running Chrome and it works fine. I was just wondering if there is an alternative to this method, which I'm sure is now ancient anyway:

This is the basic HTML, and I want the result to display in the DIV show_here

Then I would just have a PHP script (process_result.php) to display the proper code based on the selection fed to it. It's pretty basic and it's worked fine for a while, but now apparently it's not working so good.

Is there something better that should be used now? Or is there something inherently wrong with this code/method?

1. You don't need the browser differentiation any more. Since IE7 each browser perfectly supports the XMLHttpRequest object
2. You should assign onreadystatechange after the call to open
3. You need to make parameters and their values URI compliant. There is encodeURIComponent() to assure this
4. You should check for both readyState==4 and status=200 for a successful request
5. You should use onchange for a select box instead of onclick

Is there a practical reason for this or is this just preference? If you set the event listener before, it should just remain dormant until it's needed, so I don't see what difference this makes.

It can make a difference if you send more than one successive requests, especially for (older versions of) Internet Explorer. It fired the readystatechange event with readyState==4 even though it had just been assigned (before .open() was called).

It can make a difference if you send more than one successive requests, especially for (older versions of) Internet Explorer. It fired the readystatechange event with readyState==4 even though it had just been assigned (before .open() was called).

I tried researching this and found sources indicating that this is an IE6 and IE7 issue without much detail. Every complete example I saw exhibiting the issue used the ActiveX object.

The Microsoft documentation says to create the listener after the open method is invoked, but is also light on details. It indicates that this allows some kind of reset, but provides no examples. (I sent them an email asking them to update the article.)

The W3C spec always puts the event listener before open() or after send(). Putting it after doesn't make much sense to me since you may miss states if they occur before the event listener gets assigned.

I'd just explicitly reset a connection by either deleting the variable representing the request (delete symbols.request) in the above example, creating a re-usable function that automatically reinitializes the variables, or by invoking the abort function to explicitly terminate the connection.

For every complex problem, there is an answer that is clear, simple, and wrong.