Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Ajax not working?

Posted on 2014-01-28
9
Medium Priority
?
333 Views
Last Modified: 2014-01-28
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
0
Comment
Question by:Richard Korts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39815694
Seems to be working fine for me.
What makes you think it is not working?
0
 

Author Comment

by:Richard Korts
ID: 39815709
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
0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 39815717
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Accepted Solution

by:
Slick812 earned 2000 total points
ID: 39815796
greetings  rkorts, ,  You have very many things of Code in your ajax setup (browser and server side code) that are incorrect or just barely functional, I would think the Main Problem is this line of code in JS -
       document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

as a Javascript technicality the FORM inputs as a -
<input type="hidden" name="txtHint" id = "txtHint">
 do NOT have any innerHTML , as an Input it has a "value" that can be read or write to, SO, this may can work -
       document.getElementById("txtHint").value=xmlhttp.responseText;

also in your Ajax connection you have some simple altervives -
     xmlhttp=new XMLHttpRequest();
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

these really need to be in  javascript  "try" catch blocks, because in a few browsers there may be problems and no connection is made.

AND  for page development and testing, you need to have a visible PAGE output to tell you what is returned from the AJAX request, so you know that the correct response is being retrieved, otherwise you are Blind to what's happening and ask questions here on experts, just to get some idea what it does, as you say - "I can't seem to detect how to see it"
Can help with ajax code if you need that, HOWEVER, there are a thousand javascript Ajax add-on and librarys that do Ajax, and are fully tested.
But you really do not even need to do any AJAX, for this as you already have the First and Last names in the SELECT, all you have to do is use JS to change the value of the hidden input to the names from the select.
Ask questions if you need more information!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39815834
The "modern" approach to AJAX is to use jQuery (since about 2005).  This article teaches the basics.
http://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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39815835
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!!
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39815837
haha - Ray - at least we're on the same wavelength :)
0
 

Author Comment

by:Richard Korts
ID: 39815885
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.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39816097
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.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question