2011-01-23 77 views
0

我正在使用下面的代码来从我的数据库中提取信息,在选项菜单中,数据显示,但由于某些原因未将值传递到使用ajax提取信息。请指教。AJAX + PHP数据搜索

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 

     ?> 

回答

1

看来你不关闭while循环在你的 “ajax.php”。 其余的代码似乎没问题。

+0

你的意思是}后面的$ full_name? – AAA 2011-01-23 14:13:07

+1

是的,Php现在必须出现语法错误,而不是执行你的代码 – CronosS 2011-01-23 14:16:20

1

现在它的工作。你的代码有两个错误。第一个我纠正平变化=“showUser(THIS.VALUE);和第二一个回波$行[ '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 
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); 
      } 
     }); 
    });