I am currently working on Ajax search suggest input and i am done with it but i just want to make this script when you select suggestion to auto submit the form.
Here is my Javascript:
<script>
function suggest(inputString){
if(inputString.length == 0) {
$('#suggestions').fadeOut();
} else {
$.ajax({
url: "autosuggest.php",
data: 'act=autoSuggestUser&queryString='+inputString,
success: function(msg){
if(msg.length >0) {
$('#suggestions').fadeIn();
$('#searchBoxSuggestions').html(msg);
$('#country').removeClass('load');
}
}
});
}
}
function fill(thisValue) {
$('#keyword').val(thisValue);
setTimeout("$('#suggestions').fadeOut();", 600);
}
function fillId(thisValue) {
$('#country_id').val(thisValue);
setTimeout("$('suggestionsBox').fadeOut();", 600);
}
</script>
Here is my HTML form:
<form method="get" action="search.php" autocomplete="on" name="form_search">
<input type="text" size="25" value="" id="keyword" name="keyword" onkeyup="suggest(this.value);" onblur="fill();fillId();" class="" />
<input type="hidden" name="country_id" id="country_id" value="" />
<input type="submit" value=" " id="searchbutton" />
<div class="suggestionsBox" id="suggestions" style="display: none;">
<div id="searchBoxSuggestions"> </div>
</div>
</form>
And autosuggest.php if needed:
<?php
if(isset($_REQUEST['act']) && $_REQUEST['act'] =='autoSuggestUser' && isset($_REQUEST['queryString'])) {
$db_host = 'localhost';
$db_user = 'root';
$db_password = 'mypass';
$db_name = 'mydb';
$connect = mysql_connect($db_host, $db_user ,$db_password);
$db = mysql_select_db($db_name,$connect);
mysql_query("SET NAMES utf8");
if($db){
$string = '';
$queryString = $_REQUEST['queryString'];
$query = "SELECT * FROM videos WHERE title like '%" .$queryString . "%' and approved='yes' ORDER BY title LIMIT 10";
$resource = mysql_query($query);
if($resource && mysql_num_rows($resource) > 0) {
$string.= '<ul>';
while($result = mysql_fetch_array($resource)){
$title= addslashes($result['title']);
$indexer= addslashes($result['indexer']);
$string.= '<li onClick="fillId(\''.$indexer.'\');fill(\''.$title.'\');"><b>'.$title.'</b></li>';
}
$string.= '</ul>';
} else {
$string.= '<li>No Record found</li>';
}
echo $string;
exit;
}
exit;
}
?>
Now with this code when you type into the input field with name keyword
it's giving you bellow suggestions with titles.
And now when you click on any suggested title it's putting that title into the input field where you are typing and when you press the submit button it's trigering the search.
How i can make this scirpt to auto submit the form when you click on title insted of putting that title into the input?
Thanks in advance!