【问题标题】:AJAX + PHP Data SearchAJAX + PHP 数据搜索
【发布时间】:2011-06-14 00:43:26
【问题描述】:

我正在使用下面的代码从我的数据库中提取信息,在选项菜单中,数据显示,但由于某种原因,该值没有被传递以使用 ajax 提取信息。请指教。

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

        ?>

【问题讨论】:

    标签: php javascript mysql database ajax


    【解决方案1】:
    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);
               }  
           });
      });
    

    【讨论】:

      【解决方案2】:

      您似乎没有在“ajax.php”中关闭您的while 循环。 其余的代码好像没问题。

      【讨论】:

      • 你的意思是 } 在 $full_name 之后?
      • 是的。 php 现在必须出现语法错误并且不执行您的代码。
      • 做到了,但仍然无法正常工作。猜猜我永远也想不通这个 ajax 代码!
      • 打印后还缺少一个分号。妈的错,之前没注意。无论如何,您的 ajax 代码有效。问题显然来自 ajax.php。
      【解决方案3】:

      现在它的工作。您的代码中有两个错误。第一个我更正 onchange="showUser(this.value); 和第二个 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'];
      }
      ?>
      

      索引.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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-07
        • 1970-01-01
        • 2016-12-02
        • 2012-06-11
        • 2017-07-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多