I have this input=[text]
with autocomplete that offers the title with a link (a href =) in the mysql database, however I would like to create 'categories' to select the links that autocomplete offers through a dropdown select. / p>
This is my form:
<form>
<div class="form-group">
<div class="input-group">
<input id="txtSearch" class="form-control input-lg" type="text" placeholder="Selecione Região e busque por Nome..." />
<div class="input-group-addon">
<i class="glyphicon glyphicon-search"></i>
</div>
</div>
</div>
</form>
<script>
$(document).ready(function(){
$('#txtSearch').autocomplete({
source: "post_search.php",
minLength: 2,
select: function(event, ui) {
var url = ui.item.id;
if (url != '#') {
location.href = url
}
},
open: function(event, ui) {
$(".ui-autocomplete").css("z-index", 1000)
}
})
});
</script>
This is my post_search.php:
<?php
require_once 'dbconfig.php';
$keyword = trim($_REQUEST['term']); // this is user input
$sugg_json = array(); // this is for displaying json data as a autosearch suggestion
$json_row = array(); // this is for stroring mysql results in json string
$keyword = preg_replace('/\s+/', ' ', $keyword); // it will replace multiple spaces from the input.
$query = 'SELECT postID, postTitle, postUrl FROM tbl_posts WHERE postTitle LIKE :term'; // select query
$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':term'=>"%$keyword%"));
if ( $stmt->rowCount()>0 ) {
while($recResult = $stmt->fetch(PDO::FETCH_ASSOC)) {
$json_row["id"] = $recResult['postUrl'];
$json_row["value"] = $recResult['postTitle'];
$json_row["label"] = $recResult['postTitle'];
array_push($sugg_json, $json_row);
}
} else {
$json_row["id"] = "#";
$json_row["value"] = "";
$json_row["label"] = "Nothing Found!";
array_push($sugg_json, $json_row);
}
$jsonOutput = json_encode($sugg_json, JSON_UNESCAPED_SLASHES);
print $jsonOutput;
EDIT: Here's an idea of what it would look like after inserting the dropdown of categories before inputting text with the URL's:
<form>
<select class="border-white -rounded"> <!-- Esta seriam as categorias dos POSTS -->
<option>Selecione a Categoria:</option>
<option>Categoria 1</option>
<option>Categoria 2</option>
<option>Categoria 3</option>
</select>
<!-- Este seria o input para o título dos POSTS -->
<input class="border-white -rounded" type="text" placeholder="Busque por Título">
<div class="autocomplete">
<i class="glyphicon glyphicon-search"></i>
</div>
<!-- Aqui apareciam as sugestões conforme digita acima -->
</form>