0
I have this input=[text]
with autocomplete that offers the title with a link (a href=) in the mysql database, but would like to create 'categories' to select the links that the autocomplete offers through a select dropdown.
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 categories dropdown before the text input 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>
Thanks for the best organization in Anderson codes.
– Paulo Lima
Okay, let’s understand. You have the
input
search, which has the autocomplete tool when you type more than two characters. The suggestions that appear are the titles of posts that return from the database search performed in the PHP file. When a suggestion is selected, the person is forwarded to the URL of the post. What do you need is for these suggestions to appear by categories? If so, based on what would the categories be defined? The tabletbl_posts
has the category field?– Woss
That’s right, Anderson, just about the headlines and URL of the posts. But I still don’t have the categories, everything is in the same SQL, and I would like to separate this by categories, being arranged in the dropdown select, and as selected, 'limit' the suggested content in input text in URL form. If you prefer we can talk privately if you provide services in this matter. Thank you so much for now.
– Paulo Lima
Which
dropdown select
? I would still create this element in HTML?– Woss
Anderson, I edited the question because now I understand that I have left my need for development very wide.
– Paulo Lima
Always use the "Sample Codes" tool to present them. Let the tool "Snippets ..." only when the code is independent and executable.
– Woss