使用PHP,Ajax从Mysql中获取数据

时间:2015-02-27 19:09:50

标签: php jquery mysql ajax

我的数据库包含字段branch,name,code_member,status等。

我正在尝试使用分组mysql查询在下拉菜单中获取名称。然后显示在下拉列表中选择的NAME的详细信息。但没有显示结果。

选择分支,名称和自动输入名称在搜索框中工作正常。但在此ID之后,名称,分支,代码等数据未显示。

使用了三个页面。 index.php,findName.php和searchfinal.php

我试过的代码Is As Follows:

index.php:

<html>
<head>
   <script type="text/javascript">
     $(document).ready(function()
       {
          $(".branch").change(function()
       {
            var branch=$(this).val();
            var dataString = 'branch='+ branch;

     $.ajax
           ({
                 type: "POST",
                 url: "findName.php",
                 data: dataString,
                 cache: false,
                 success: function(html)
              {
              $(".getname").html(html);
              } 
            });
         });
        });
   </script>

   <script>
         $(document).ready(function()
             {
                  $('#getname').change(function() {
                  $('#name').val($(this).val());
                });
             });
   </script>

</head>

<body>

  Branch :
     <select name="branch" class="branch">
        <option selected="selected">--Select Country--</option>
     <?php
       $sql=mysql_query("select branch from mutualbs group by branch");
           while($row=mysql_fetch_array($sql))
       {

              $branch=$row['branch'];
                 echo '<option value="'.$branch.'">'.$branch.'</option>';
       } ?>
     </select> <br/><br/>

   Name :
     <select name="getname" id="getname" class="getname">
             <option selected="selected">--Select Name--</option>
     </select>

   <br><br>

   <hr>

   <form method="get">   
         <label for="name">Name To Search :</label>
         <input type="text" id="name" name="name" />

         <button class="btnSearch">Search</button>   
   </form>
   <br><br>

   <table id="resultTable" style="color:#ff0000;">
     <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>MBS Number</th>
            <th>Branch</th>
            <th>Status</th>
       </tr>
     </thead>
            <tbody></tbody> //** Results Are Not Getting Shown
   </table>

    <script src="../js/jquery-1.10.2.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <script>
      $jQuery(document).ready(function($) {
            $('.btnSearch').click(function(){
                makeAjaxRequest();
            });

            $('form').submit(function(e){
                e.preventDefault();
                makeAjaxRequest();
                return false;
            });

      function makeAjaxRequest() {
             $.ajax({
                    url: 'searchfinal.php',
                    type: 'get',
                    data: {name: $('input#name').val()},
                    success: function(response) {
                       $('table#resultTable tbody').html(response);
                    }
                 });
              }
            });
      </script>

   </body>
</html>

直到搜索按钮一切正常...但在点击搜索按钮后,没有结果,即ID,名称,代码,分支和状态显示..

其他代码:

searchfinal.php

<?
require_once 'Connection.simple.php';
$conn = dbConnect();
    if (isset($_GET['name'])) {
    $data = "%".$_GET['name']."%";
    $sql = "SELECT * FROM mutualbs WHERE name like ?";
    $stmt = $conn->prepare($sql);
    $results = $stmt->execute(array($data));
    $rows = $stmt->fetchAll();
   }
   if(empty($rows)) {
    echo "<tr>";
       echo "<td colspan='4'>There were not records</td>";
    echo "</tr>";
   }
   else {
       foreach ($rows as $row) {
       echo "<tr>";
       echo "<td>".$row['id']."</td>";
       echo "<td>".$row['name']."</td>";
       echo "<td>".$row['code_number']."</td>";
       echo "<td>".$row['branch']."</td>";
       echo "<td>".$row['status']."</td>";
       echo "</tr>";
    }
 }
 ?>

需要帮助......

0 个答案:

没有答案