dongmangsha7354 2018-12-10 11:33
浏览 326

如何使用PHP上的输入搜索下拉列表

hope all be will. I face to like this problem anyone can help me. I get the value of dropdown and I put it to input, but I am unable to search it. I used from jQuery to get the value from bootstrap dropdown to give for input.

  <!-- HTML Code With Form --> 
  <!-- Basic dropdown -->
  <form method="post" enctype="multipart/form-data" action="team.php" 
  class="form-inline active-purple-3 active-purple-4" style="width:400px; 
  margin-top:-50px; margin-left:125px; ">

  <button id="sb" class="btn btn-primary dropdown-toggle mr-4" type="button" 
  data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" 
  style="margin-top:20px; margin-left:-140px;">Search By</button>

  <div class="dropdown-menu"  id="dm" style="margin-left:89px; margin- 
  top:-963px;">

  <a class="dropdown-item" id="dl" href="#" value="ID">BY ID</a>
  <a class="dropdown-item" id="dl" href="#" value="Name">Name</a>
  <a class="dropdown-item" id="dl" href="#" value="Positon">Position</a>
  </div>
  <input type='hidden' name='thenumbers'>
  <!-- Basic dropdown -->
  <!-- Search form -->
  <i class="fa fa-search" aria-hidden="true" style="margin-top:15px;"></i>
  <input name="txtSearch" class="form-control form-control-sm ml-3 w-75" 
  id="in" type="text" placeholder="Search" style=" margin-top:15px; border- 
  radius:5px 5px 5px 5px;" aria-label="Search">
  <button type="Submit" name="searchrec" class="btn btn-primary" 
  style="margin-left:365px; margin-top:-55px;">Search</button>
  </form>



  <!-- jQuery Code -->
   <script>
   $(document).ready(function(){
   $('#dm a').on('click', function(){
   $('#sb').text($(this).text());
    });
    });
   </script>
   <script>
   $(function(){
 //Listen for a click on any of the dropdown items
  $("#dm a").click(function(){
    //Get the value
    var value = $(this).text();
    //Put the retrieved value into the hidden input
    $("input[name='thenumbers']").val(value);
   });
   });
   </script>
  <!-- jQuery Code End -->


   <!-- PHP Code  -->
   <?php
   include "Config.php";
   if(isset($_POST['searchrec']))
   {
  $txtSearch=$_POST['txtSearch'];
  $column=$_POST['thenumbers'];

 if($column == "BY ID")
 {

 $query1="SELECT * FROM ourteam WHERE id LIKE '%$txtSearch%'";
 $result1=mysqli_query($db,$query1);
 $count=mysqli_num_rows($result1);
 if($count == 0)
 {

 }else{
 while($rowr=mysqli_fetch_array($result1))
 {
     $id=$rowr['id'];
     $name=$rowr['name'];
     echo "<script>alert($name);</script>";
 }
 }

This PHP code is just for ID and I used elesif statement also for other entities.

  • 写回答

1条回答 默认 最新

  • douqi3913 2018-12-10 11:48
    关注

    First, get the values not the text:

    //Get the value
    var value = $(this).attr('value'); // instead of text
    

    Then, map it on the server side

    $allowed_columns = ['id', 'name', 'position'];
    $column = $_POST['thenumbers'];
    $txtSearch = $_POST['txtSearch'];
    
    // Check the field name
    if (!in_array($column, $allowed_columns)) {
        // Throw some exception, or do whatever you want
    }
    
    // Sanitize the value
    $txtSearch = mysqli_real_escape_string($txtSearch);
    
    $query1="SELECT * FROM ourteam WHERE `$column` LIKE '%$txtSearch%'";
    
    // ...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算