473,372 Members | 909 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 4021
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

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

Similar topics

12
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, without having to refresh or go to a seperate page,...
5
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 solve: With AJAX you typically update/replace only...
3
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 reloading the page. The specific audio file name is...
6
jafarsalam
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 : http://www.w3schools.com/php/php_ajax_database.asp I downloaded the code and...
3
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 local I got the text from db with Correct...
21
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 FIREFOX will support AJAX or not
1
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.
1
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 didn't know anything about ajax and could you please...
1
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 section where the Admin can INSERT new records in...
2
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 corresponding record using ajax and php. But here...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.