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



Im trying to connect AJAX with PHP/MYSQL. I wrote a basic code for connecting this. I got codings from

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 I wrote the correct address. But im getting the error…

Will any one help me to correct this?

Below i list out the codings


<title>Connecting with DB</title>
<script type="text/javascript" src="ajax_db.js"> </script>
Select a person
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>


function showUser(str)
// document.write(str);
if (str=="")
if (window.XMLHttpRequest)
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (xmlhttp.readyState==4 && xmlhttp.status==200)
}catch(e) {
alert("Unable to connect with the server:\n" +e.toString());


$q = $_GET["q"];
echo $q;
$con = mysql_connect("", "DB", "bbscn107");
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("TEST", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
echo $sql;
$result = mysql_query($sql);
echo "<table border='1'>
while($row = mysql_fetch_array($result))
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
echo "</table>";


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.


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…


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 (, some security thing or whatever. If you’ve got all 3 files in the same folder, then you need to remove this line:‘GET’,’’+str,true);

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.


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.‘GET’,’getuser.php?q=’+str,true);

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

My object wouldnt call the PHP program…



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.


Sorry… I have all 3 files in the same folder… But i cant able to run it… Im getting the error msg… 🙁