2017-05-06 59 views
1

我想创建一个搜索功能。这个页面已经有一些细节,我从数据库中检索div。我想当我点击搜索按钮时,隐藏所有检索到的细节并显示搜索到的细节。我为它使用了ajax。现在问题是当我点击搜索按钮时,先前检索到的数据是隐藏的,但不会显示为搜索结果。通过php中的ajax显示数据

<form action="../PHP/searchrmvvac.php" method="post"> 
        <div class="search hidden-xs hidden-sm"> 
         <input type="text" placeholder="Search" id="search" name="search"> 
        </div> 
        <div> 
         <input type="button" value="Search" id="searchrmvcom" name="searchrmvcom"> 
         <script> 
          $("#searchrmvcom").click(function() { 
           var comname=$('#search').val(); 
           $.ajax({ 
            type:"post", 
            url:"../PHP/searchrmvvac.php", 
            data:{comname:comname}, 
            success:function (data3) { 
             $('#rmvcomdiv').hide(); 
             $('#ela').html(data3) 
            } 
           }); 
          }); 
         </script> 
        </div> 
        </form> 

searchrmvvac.php

<?php 
    session_start(); 
    require('../PHP/dbconnection.php'); 
    $output=$_POST['comname']; 
    $sql="select * from company where company_name='$output' and activation_code=1"; 
    $res=mysqli_query($conn,$sql); 
    if(mysqli_num_rows($res)>0) { 
    echo ' 
while ($row = mysqli_fetch_assoc($res)) { 
    ?> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> 
    <table class="table table-striped table-bordered table-list"> 
     <thead> 

     <tr> 
      <th>Action</th> 
      <th>ID</th> 
      <th>Registration number</th> 
      <th>Company Name</th> 
      <th>Email</th> 
     </tr> 
     </thead> 
     <tbody> 

      <tr> 
       <td align="center"><button type="submit" class="btn btn-default myButton" value="$row['/companyid/']" id="accept" name="accept">Remove</button></td> 
       <td>$row['/companyid/']</td> 
       <td>$row['/government_reg_no/']</td> 
       <td>$row['/company_name/']</td> 
       <td>$row['/email/']</td> 
      </tr> 

     </tbody> 
    </table> 

    <?php 
} 
'; 
} 
?>[![enter image description here][1]][1] 

线27

<td align="center"><button type="submit" class="btn btn-default myButton" value="$row['/companyid/']" id="accept" name="accept">Remove</button></td> 

+0

你的问题是什么?请清楚解释你的问题,包括你的*预期*比。 *当前*输出等。 –

+0

是否有代码缺失或错字? 'echo'while($ row = mysqli_fetch_assoc($ res)){' - >你是否尝试回显循环? – OldPadawan

+1

请使用'error_reporting(E_ALL); ini_set('display_errors',1);'在你的页面顶部,让我们知道PHP告诉你什么,如果错误... – OldPadawan

回答

0

有你的代码一些问题,如:

  • 你没有在你的代码中包含/引用任何jQuery库,所以$("#searchrmvcom").click(...首先不起作用。
  • 把你的JavaScript/jQuery代码嵌入到你的HTML表单中并没有意义,而是将它们分开。
  • searchrmvvac.php页面中没有JavaScript/jQuery代码,所以在这里没有必要包含jQuery库。
  • 您以错误的方式将PHP变量$row['...']嵌入HTML表格中。加上你错误的方式。

你的表单页面会是这样的:

<form action="../PHP/searchrmvvac.php" method="post"> 
    <div class="search hidden-xs hidden-sm"> 
     <input type="text" placeholder="Search" id="search" name="search"> 
    </div> 
    <div> 
     <input type="button" value="Search" id="searchrmvcom" name="searchrmvcom"> 
    </div> 
</form> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> 
<script> 
    $("#searchrmvcom").click(function() { 
     var comname=$('#search').val(); 
     $.ajax({ 
      type:"post", 
      url:"../PHP/searchrmvvac.php", 
      data:{comname:comname}, 
      success:function (data3) { 
       $('#rmvcomdiv').hide(); 
       $('#ela').html(data3) 
      } 
     }); 
    }); 
</script> 

随后,您searchrmvvac.php页面会是这样的:

<?php 
    session_start(); 
    require('../PHP/dbconnection.php'); 
    $output=$_POST['comname']; 
    $sql="select * from company where company_name='$output' and activation_code=1"; 
    $res=mysqli_query($conn,$sql); 
    if(mysqli_num_rows($res)>0) { 
     while ($row = mysqli_fetch_assoc($res)) { 
      ?> 
      <table class="table table-striped table-bordered table-list"> 
       <thead> 

       <tr> 
        <th>Action</th> 
        <th>ID</th> 
        <th>Registration number</th> 
        <th>Company Name</th> 
        <th>Email</th> 
       </tr> 
       </thead> 
       <tbody> 

        <tr> 
         <td align="center"><button type="submit" class="btn btn-default myButton" value="<?php echo $row['companyid']; ?>" id="accept" name="accept">Remove</button></td> 
         <td><?php echo $row['companyid']; ?></td> 
         <td><?php echo $row['government_reg_no']; ?></td> 
         <td><?php echo $row['company_name']; ?></td> 
         <td><?php echo $row['email']; ?></td> 
        </tr> 

       </tbody> 
      </table> 
      <?php 
     } 
    } 
?> 

旁注:了解大约prepared statement因为现在你的查询易感e到SQL注入攻击。另见how you can prevent SQL injection in PHP

相关问题