dpsx99068 2018-10-08 19:54
浏览 55

自动填充要在表单中使用的文本框

i'm trying to create an autocomplete box for a form. Basically what I want to achieve is a textarea field that allows me to search the database for names belonging to a table and add them to a table through a form. I managed to create this, but it allows me to search only one name at a time, while I would need to look for more names to add to the same textarea.

Basically I have a textarea and this must give me the possibility to insert more names: name1 name2 ... Where each name is chosen from the drop-down menu that is created. In this way I could go to insert more "names" in the database.

auto_complete.php

<!DOCTYPE html>  
 <html>  
      <head>  
           <title>Autocomplete TextBox</title>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <style>  
           ul{  
                background-color:#eee;  
                cursor:pointer;  
           }  
           li{  
                padding:12px;  
           }  
           </style>  
      </head>  
      <body>  
           <br /><br />  
           <div class="container" style="width:500px;">  
                <form method="post" action="insert_db.php">
                <h3 align="center">Box Text</h3><br />  
                <label>Enter  Name</label>  
                <br>&ensp;<textarea name="nominativo" rows="5" cols="30" placeholder="Name Surname" id="nominativo" class="form-control""  ></textarea><br>
                <input type="submit" value="Invia">
                <div id="nominativoList"></div>  
                </form>
           </div>  
      </body>  
 </html>  
 <script>  
 $(document).ready(function(){  
      $('#nominativo').keyup(function(){  
           var query = $(this).val();  
           if(query != '')  
           {  
                $.ajax({  
                     url:"search.php",  
                     method:"POST",  
                     data:{query:query},  
                     success:function(data)  
                     {  
                          $('#nominativoList').fadeIn();  
                          $('#nominativoList').html(data);  
                     }  
                });  
           }  
      });  
      $(document).on('click', 'li', function(){  
           $('#nominativo').val($(this).text());  
           $('#nominativoList').fadeOut();  
      });  
 });  
 </script>

search.php

<?php  
$conn = mysqli_connect("localhost", "root", "123456789", "Personale");  


 if(isset($_POST["query"]))  
 {  
      $output = '';  
      $query = "SELECT * FROM Squadra WHERE Nominativo LIKE '%".$_POST["query"]."%'";  
      $result = mysqli_query($conn, $query);  
      $output = '<ul class="list-unstyled">';  
      if(mysqli_num_rows($result) > 0)  
      {  
           while($row = mysqli_fetch_array($result))  
           {  
                $output .= '<li>'.$row["Nominativo"].'</li>';  
           }  
      }  
      else  
      {  
           $output .= '<li>Country Not Found</li>';  
      }  
      $output .= '</ul>';  
      echo $output;  
 }  

 ?>  

insert_db.php

<?php
$conn = mysqli_connect("localhost", "root", "123456789", "Personale");  
$nominativo = $_POST['nominativo'];

$sql = "INSERT INTO Prenotazione (Nominativo) VALUES ('$nominativo')";
$result = mysqli_query($conn,$sql);


?>

In your opinion how can i do?? Thank you for your attention and for your time.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭