0

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

        ?>

3 Answers 3

2
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);
           }  
       });
  });
Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

you mean } after the $full_name?
Yes. Php must rise a syntax error for now and not execute your code.
Did it but its still not working. Guess i'll never figure this ajax code out!
There's also a semicolon missing after your print. Ma fault, I didn't notice before. Anyway, your ajax code works. The prob clearly comes from ajax.php.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.