472,353 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

How to fix "Unable to connect with the server:" in simple AJAX script?

Im trying to connect AJAX with PHP/MYSQL. I wrote a basic code for connecting this. I got codings from http://www.w3schools.com/PHP/php_ajax_database.asp.

1) I created database and tables.
2) While i run the ajax_db.html, if i select anything, im getting

Unable to connect with the server:
[Object error]

I used document.write message and i checked the select name number is passing in the java script. But i doesnt call the xmlhttp.open(). I wrote the correct address. But im getting the error...

Will any one help me to correct this?

Below i list out the codings

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Connecting with DB</title>
  4. <script type="text/javascript" src="ajax_db.js"> </script>
  6. </head>
  8. <body>
  10. <form>
  11.   Select a person
  12.     <select name="users" onchange="showUser(this.value)">
  13.         <option value="">Select a person:</option>
  14.         <option value="1">Peter Griffin</option>
  15.         <option value="2">Lois Griffin</option>
  16.         <option value="3">Glenn Quagmire</option>
  17.         <option value="4">Joseph Swanson</option>
  18.     </select>
  19. </form>
  21. <br />
  22. <div id="txtHint"><b>Person info will be listed here.</b></div>
  24. </body>
  25. </html> 
Expand|Select|Wrap|Line Numbers
  1. function showUser(str)
  2.  { 
  3.   // document.write(str);
  5.    if (str=="")
  6.    {
  7.        document.getElementById("txtHint").innerHTML="";
  8.        return;
  9.    } 
  10.    if (window.XMLHttpRequest)
  11.    {
  12.        // code for IE7+, Firefox, Chrome, Opera, Safari
  13.        xmlhttp=new XMLHttpRequest();       
  14.    }
  15.    else
  16.    {  
  17.        // code for IE6, IE5
  18.        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");       
  19.    }
  20.    xmlhttp.onreadystatechange=function()
  21.    {
  22.       if (xmlhttp.readyState==4 && xmlhttp.status==200)
  23.       {  
  24.          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;        
  25.       }
  26.    }
  28.  try{
  29.       //xmlhttp.open('GET','getuser.php?q='+str,true);
  30.       xmlhttp.open('GET',''+str,true);
  31.       xmlhttp.send();    
  32.     }catch(e)  {
  33.    alert("Unable to connect with the server:\n" +e.toString());
  34.  }
  35. }

Expand|Select|Wrap|Line Numbers
  1. <?php
  3.   $q = $_GET["q"];
  4.   echo $q;
  6.      $con = mysql_connect("", "DB", "bbscn107");
  7.     if (!$con)
  8.   {
  9.       die('Could not connect: ' . mysql_error());
  10.   }
  12.     mysql_select_db("TEST", $con);
  14.     $sql="SELECT * FROM user WHERE id = '".$q."'";
  15.   echo $sql;
  17.   $result = mysql_query($sql);
  19.   echo "<table border='1'>
  20.   <tr>
  21.         <th>Firstname</th>
  22.         <th>Lastname</th>
  23.         <th>Age</th>
  24.         <th>Hometown</th>
  25.         <th>Job</th>
  26.     </tr>";
  28. while($row = mysql_fetch_array($result))
  29.   {
  30.       echo "<tr>";
  31.       echo "<td>" . $row['FirstName'] . "</td>";
  32.       echo "<td>" . $row['LastName'] . "</td>";
  33.       echo "<td>" . $row['Age'] . "</td>";
  34.       echo "<td>" . $row['Hometown'] . "</td>";
  35.       echo "<td>" . $row['Job'] . "</td>";
  36.       echo "</tr>";
  37.   }
  38. echo "</table>";
  40. mysql_close($con);
  41. ?> 
Jan 24 '11 #1
6 3893
404 256MB
Have you tried making a direct request to getuser.php with a query string? For example, enter this into your browser:

Does it come up with anything? If it gives you the error, then the problem is with your database connection or query. Do that and post back the results and we'll help you more, right now there are too many things it could be.
Jan 24 '11 #2
Hi... Thanks for your reply. I typed as u said

But i got the same error

Unable to connect with the server:
[Object error]
If i type the above address in the browser, i can see the output. So now the problem is it doesnt call the PHP program.

Will u help me?

Advance Thanks...
Jan 24 '11 #3
404 256MB
All I needed to know was whether or not typing the path directly into the browser resulted in the error. I'm not 100% positive because I use jQuery, but I'm pretty sure you can't make AJAX calls to full URLs (http://something.com), some security thing or whatever. If you've got all 3 files in the same folder, then you need to remove this line:


And uncomment this line:


Do that and let me know what the result is. Also, this line:

document.getElementById("txtHint").innerHTML=xmlht tp.responseText;

Make sure there is no space in the word xmlhttp. I don't know why I didn't see that earlier, but that's probably your problem.
Jan 24 '11 #4
I checked my program. There is no space in b/w xmlhttp.
document.getElementById("txtHint").innerHTML=xmlht tp.responseText;

I kept all 2 files in the same folder. I uncomment the below line.


I tried to run the program. But i got the same error message :(

My object xmlhttp.open wouldnt call the PHP program...

Jan 24 '11 #5
404 256MB
You said all 2 files, but there should be 3, ajax_db.html, ajax_db.js, and getuser.php.

Maybe someone else will be able to help you, because I see nothing wrong with the code, you pretty much copied it word-for-word from the w3schools site.
Jan 24 '11 #6
Sorry... I have all 3 files in the same folder... But i cant able to run it... Im getting the error msg... :(
Jan 24 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

by: JMB | last post by:
Hello, I was wondering if anyone knew of any projects extending the inline upload progress bar to utilize an inpage image uploader with bar,...
by: Martin | last post by:
Hello NG, I've been doing some AJAX for a few weeks now. The basics worked fine so far, but now I've got the following problem which I can't...
by: abrtlt | last post by:
I would like to have a web page in which, when the user clicks on any of several specific elements, a specific audio file is played, without...
by: jafarsalam | last post by:
hi; I'm new to PHP and AJAX MySQL codes. I found a simple code for PHP and AJAX MySQL Database communication on :...
by: bss | last post by:
Dear All, I have developing a French website using PHP & Ajax. In that I tried to display some French texts from mysql database using Ajax. Form...
by: buss123 | last post by:
Hi, in our application we are using AJAX . but application is working in IE well but in FIREFOX it is is not opening .my doubt is wether ...
by: acl20032003 | last post by:
anyone here could show me a simple codes on deleting files from the database using ajax? any help would be appreciated. im just new to ajax.
by: ghjk | last post by:
I'm developing web application using php. In there i want to refresh one of my table which is having mysql data, using ajax. How can I do that? I...
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD...
by: raamay | last post by:
hello experts, i have fetched records from mysql database and against each record i want to place a update tag which when clicked will update the...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.