#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Cant retrieve data from db [AJAX]


    Hi, I'm trying to use AJAX to load user's profile through a drop-down list on selected user.

    There are 3 seperate files:
    1) view_info.php
    2) selectuser.js
    3) getuser.php

    I tested and found that the value $q did pass from view_info -> selectuser.js -> getuser.php, and value changes correctly as i select different user from the drop-down list.

    Problem is, it simply can't fetch data from my database using the value $q. I tried debugging it for 3 hours already and is totally out of idea whats wrong with the script. Please help me check what is wrong, i'm new to AJAX, this is my first script..

    view_info.php File:
    Code:
    <?php
    	$con = mysql_connect('yuehyih.hypermartmysql.com', 'username', 'password');
    	if (!$con)
    	 {
    	    die('Could not connect: ' . mysql_error());
    	 } 
    
    	mysql_select_db("yuehyih_ieye", $con); 
    	$sql="SELECT * FROM user";
    	$result = mysql_query($sql); 
    	?>
    	<form> 
    	Select a User:
    	<select name="users" onchange="showUser(this.value)">
    	<?php $num=1;
    	while($row = mysql_fetch_array($result))
    	{  ?>
    	<option value="<? echo $num; ?>"><?php echo $row['username'];?></option>
    	<?php
    	$num=$num+1;
    	} ?> 
    	</select>
    	</form>
    <p>
    <div id="txtHint"><b>User info will be listed here.</b></div>
    </p>
    selectuser.js File
    Code:
    var xmlHttp;
    
    function showUser(str)
    { 
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
     {
     alert ("Browser does not support HTTP Request");
     return;
     }
    var url="getuser.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    }
    
    function stateChanged() 
    { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     { 
     document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
     } 
    }
    
    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
     {
     // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
     }
    catch (e)
     {
     //Internet Explorer
     try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
     catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
     }
    return xmlHttp;
    }
    getuser.php
    Code:
    <?php
    $q=$_GET['q'];
    echo "value of q is ".$q;
    $con = mysql_connect('yuehyih.hypermartmysql.com', 'yuehyih', '000099');
    if (!$con)
     {
     die('Could not connect: ' . mysql_error());
     }
    
    mysql_select_db("yuehyih_ieye", $con);
    
    /*** i have tried with sql="SELECT * FROM user WHERE userID=' ".$q." ' ";  but still didn't work ***/
    $sql="SELECT * FROM user WHERE userID='$q'";
    
    $result = mysql_query($sql);
    
    while($row = mysql_fetch_array($result))
    {
    echo "<table border='0' width=380px> <tr><td width=50px><b>Name</b></td><td>";
    echo ":". $row['Name'] . "</td></tr>";
    echo "<tr><td><b>Email</b></td><td>";
    echo ":". $row['Email'];
    echo "</td></tr>";
    echo "<tr><td><b>Contact</b></td><td>";
    echo ":". $row['Contact'];
    echo "</td></tr>";
    echo "<tr><td><b>Contry</b></td><td>";
    echo ":". $row['Contry'];
    echo "</td></tr>";
    echo "<tr><td><b>State</b></td><td>";
    echo ":". $row['State'];
    echo "</td></tr>";
    echo "<tr><td><b>Address</b></td><td>";
    echo ":". $row['Address'];
    echo "</td></tr>";
    echo "<tr><td><b>Zipcode</b></td><td>";
    echo ":". $row['Zipcode'];
    echo "</td></tr>";
    echo "</table>";
    }
    mysql_close($con);
    ?>
    Thanks in advance!
  2. #2
  3. No Profile Picture
    I AM A GOLDEN GOD
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Apr 2003
    Location
    Camarillo, California
    Posts
    5,929
    Rep Power
    1170
    Ok, two things.

    First, if you have a `userID` field in your database that relates to each record (i.e. it is the PRIMARY KEY for that record) then you need to be using that value rather than a false incremented value represented by $num. What if you have records 100 ~ 102 and then 106 ~ 121? The SQL statement will never return a resultset because the drop down list is representing users from #1 ~ N based on a false numeric value, and those values are being sent as the GET string.

    Secondly, and this is also a big problem, your 'getuser.php' script is returning multiple lines to the Ajax request. It need to return either a single value, an XML document, or JSON. Furthermore, once the script puts out it's first echo statement, that's it. The Ajax method call retrieves that and doesn't do anything else. You have to concatenate a string together and then echo it once. SInce you're sending a table, I would bet that your calling PHP script (view_info.php) is actually just receiving the first line, which is markup!! If you use Firebug to view what the DOM is doing (since you can't just view the HTML source), I'd bet you would see an opening TABLE tag, but nothing else.

    Comments on this post

    • boygenius disagrees : i seriously doubt the authenticity of the second statement coz i myself have used several echo statements with AJAX and never faced any problem
    "Seriously, we're not a search engine, we're actual people." ~ ManiacDan

    BookMooch.com : Give books away. Get books you want.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0


    i'm getting this output after selecting the user from dropdown list. It is able to echo all the pre-defined fields in front but couldnt' fetch data from my db. and also, value of q is 2, which is properly passed from previous forms.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    144
    Rep Power
    26
    apart from the first problem mentioned by lnxgeek the code seems to be fine

    just as test make a query to the database such as "SELECT * FROM user" i.e. without any conditional clause ans see if it works . ALso try making other queries on the get_user page to see if database is accessible.
    thirdly replace the field names by array indices
    i.e.
    $row['name']---$row[0] etc
  8. #5
  9. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2008
    Location
    North Carolina
    Posts
    2,670
    Rep Power
    2679
    I dont have time to take a look at the complete thing right now, but one thing I noticed as I skimmed over it is that you are open to SQL injection.

    Since $q is coming from the URL, you need to escape it with atleast mysql_real_escape_string(), else someone could very easily get whatever they want out of the database.
  10. #6
  11. No Profile Picture
    I AM A GOLDEN GOD
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Apr 2003
    Location
    Camarillo, California
    Posts
    5,929
    Rep Power
    1170
    Originally Posted by boygenius
    i seriously doubt the authenticity of the second statement coz i myself have used several echo statements with AJAX and never faced any problem


    Made me chuckle, thanks.

    Seems as if you're right though, I'm really surprised that multiple echo statements in a while loop (why is there a loop for one record again?) actually returns properly to the XHR call. I'm going to do a test case.
    "Seriously, we're not a search engine, we're actual people." ~ ManiacDan

    BookMooch.com : Give books away. Get books you want.

IMN logo majestic logo threadwatch logo seochat tools logo