469,332 Members | 6,649 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

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

Hi...
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

ajax_db.html
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>
  5.  
  6. </head>
  7.  
  8. <body>
  9.  
  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>
  20.  
  21. <br />
  22. <div id="txtHint"><b>Person info will be listed here.</b></div>
  23.  
  24. </body>
  25. </html> 
ajax_db.js
Expand|Select|Wrap|Line Numbers
  1. function showUser(str)
  2.  { 
  3.   // document.write(str);
  4.  
  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.    }
  27.  
  28.  try{
  29.       //xmlhttp.open('GET','getuser.php?q='+str,true);
  30.       xmlhttp.open('GET','http://192.168.100.152/~rekha/AJAX/getuser.php?q='+str,true);
  31.       xmlhttp.send();    
  32.     }catch(e)  {
  33.    alert("Unable to connect with the server:\n" +e.toString());
  34.  }
  35. }

getuser.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.   $q = $_GET["q"];
  4.   echo $q;
  5.  
  6.      $con = mysql_connect("192.168.100.152", "DB", "bbscn107");
  7.     if (!$con)
  8.   {
  9.       die('Could not connect: ' . mysql_error());
  10.   }
  11.  
  12.     mysql_select_db("TEST", $con);
  13.  
  14.     $sql="SELECT * FROM user WHERE id = '".$q."'";
  15.   echo $sql;
  16.  
  17.   $result = mysql_query($sql);
  18.  
  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>";
  27.  
  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>";
  39.  
  40. mysql_close($con);
  41. ?> 
  42.  
Jan 24 '11 #1
6 3612
HaLo2FrEeEk
404 256MB
Have you tried making a direct request to getuser.php with a query string? For example, enter this into your browser:

http://192.168.100.152/~rekha/AJAX/getuser.php?q=1

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
http://192.168.100.152/~rekha/AJAX/getuser.php?q=1

But i got the same error

Unable to connect with the server:
[Object error]

http://192.168.100.152/~rekha/AJAX/getuser.php?q=1
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
HaLo2FrEeEk
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:

xmlhttp.open('GET','http://192.168.100.152/~rekha/AJAX/getuser.php?q='+str,true);

And uncomment this line:

//xmlhttp.open('GET','getuser.php?q='+str,true);

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.

xmlhttp.open('GET','getuser.php?q='+str,true);

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

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

yyy?????
Jan 24 '11 #5
HaLo2FrEeEk
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

Post your reply

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

Similar topics

5 posts views Thread by Martin | last post: by
3 posts views Thread by abrtlt | last post: by
3 posts views Thread by bss | last post: by
1 post views Thread by ghjk | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.