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