AAA
AAA

Reputation: 3168

AJAX + PHP Data Search

I am using the code below to to pull information from my database, in the option menu, the data shows but for some reason the value is not being passed on to pull information using ajax. Please advise.

PHP:

 <html>
 <head>
 <script type="text/javascript">
 function showUser(str)
 {
 if (str=="")
 {
 document.getElementById("txtHint").innerHTML="";
  return;
  }
 if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
 else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
 {
 document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  }
  }
 xmlhttp.open("GET","ajax.php?q="+str,true);
  xmlhttp.send();
  }
  </script>
 </head>
 <body>
<select onchange="display_data(this.value);">
<option>Select user</option>
<?php
include("config.php");

$query="SELECT p_name, full_name FROM users order by p_name asc";
$result=mysql_query($query);
while(list($p_name, $full_name)=mysql_fetch_row($result)) {
    echo "<option value=\"".$p_name."\">".$full_name."</option>";
}

?>
</select>
<div id="txtHint"><div>
</body>
</html>

Ajax.php

 <?php
 $q=$_GET["q"];

 include("config.php");


 //Query
 $sql = "select `full_name` from `users` where `p_name` = '$q'";
 $query = mysql_query($sql) or die ("Error: ".mysql_error());

    while ($row = mysql_fetch_array($query)){

 $full_name = $row['Full_name'];


        print $full_name

        ?>

Upvotes: 0

Views: 1973

Answers (3)

Hemant Jain
Hemant Jain

Reputation: 23

Jquery Ajax call through data serach from php file  

$('#btnSubmit').click(function() {  


   var name = $('#Name').val();

       $.ajax({  
           type: "POST",  
           url: "search.php",
           data: "name="+ name,  
           success: function(response){ 
            $('#search').html(response);
           }  
       });
  });

Upvotes: 2

user514310
user514310

Reputation:

Now its working. There was two mistakes in you code. First one I correct onchange="showUser(this.value); and second one echo $row['full_name']; } Ajax.php

 <?php
$q=$_GET["q"];
include("config.php");
//Query
$sql = "select `full_name` from `users` where `p_name` = '$q'";
$query = mysql_query($sql) or die ("Error: ".mysql_error());
while ($row = mysql_fetch_array($query)){
    echo $row['full_name'];
}
?>

Index.php

 <html>
 <head>
 <script type="text/javascript">
 function showUser(str)
 {
if (str=="")
{
 document.getElementById("txtHint").innerHTML="";
return;
}
  if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
   {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
 {
 document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
 <select onchange="showUser(this.value);">
 <option>Select user</option>
 <?php
 include("config.php");
$query="SELECT p_name, full_name FROM users order by p_name asc";
$result=mysql_query($query);
while(list($p_name, $full_name)=mysql_fetch_row($result)) {
echo "<option value=\"".$p_name."\">".$full_name."</option>";
}

?>
</select>
<div id="txtHint"><div>
</body>
</html>
enter code here

Upvotes: 1

CronosS
CronosS

Reputation: 3159

It seems that you don't close your while loop in your "ajax.php". The rest of the code seems to be ok.

Upvotes: 1

Related Questions