Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Ajax not working?

Hi,

Please see attached php files; the first one (ajax_test.php) is supposed to take the name selected from the pulldown & look it up in a corresponding table on the web server. Try running it at www.rkassoc.org/FWLSCO/ajax_test.php. It returns nothing (or if it does, I can't seem to detect how to see it).

Note I DO NOT want the result displayed directly on the page; I will use it for something else.

I suspected that the server program was at fault so I tried it directly using http://rkassoc.org/FWLSCO/getcustomer.php?q=BALCH/BRADY (I know that name is in the database). It worked properly.

What's wrong?

You may wonder why I am trying this; this is just to see if I can do it like this, the real application will be more complex.

Thanks
ajax-test.php
getcustomer.php
Avatar of Gary
Gary
Flag of Ireland image

Seems to be working fine for me.
What makes you think it is not working?
Avatar of Richard Korts

ASKER

To Cathal,

Note the Javascript function show_sel which is run onLoad; it does not run when the server process is completed. Or maybe that is not an "onLoad"?

Thanks
if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
 document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
show_sel();
}

it would not run onLoad as you have already loaded your page and later operation is performed.
You have to call the function explicitly
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The "modern" approach to AJAX is to use jQuery (since about 2005).  This article teaches the basics.
https://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html

And the code you have in there is ripe for getting your database clobbered.  You'll need to get off of MySQL anyway, since PHP is doing away with MySQL support.  This article explains why and what you must do to keep your scripts running in the future.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
May not help you out on this problem, but the way you are coding is 'old-school'.

In your PHP script you are using the deprecated mysql functions (you need to switch to mysqli or PDO)

For doing this kind of thing, unless you can come up with a very good reason not to, start using jQuery. Running AJAX scripts using jQuery, your whole javascript code becomes 2 or 3 lines of code - robust, clean and fully cross-browser compliant.

And just for my own curiousity - why on earth are you targetting IE5 and IE6!!
haha - Ray - at least we're on the same wavelength :)
To all,

With the exception of 1 or two lines, I copied the sample ajax code from a sample given at w3 schools. I have no clue about why they are concerned about IE5 & 6.

I am aware of the MySQL issue. Thank you for pointing that out.

I figured out a way to do what I need without going back to the server with Ajax.
I have no clue about why they are concerned about IE5 & 6.
It's because their code example is many years out of date.  And now that you've copied it, you've made your code many years out of date, too.  Copying the code without understanding the code is AntiPractice #1!

Next time before you copy something you don't understand, stop by EE and ask if it's a good idea to use that example, or if there is a better example available.  We have a lot of articles here that can help you keep things up-to-date, follow best practices, etc.