user7441072 user7441072 - 11 months ago 85
jQuery Question

my onselect is not working

I have a textbox with autocomplete.
I want to display one div when the value is selected in textbox. I tried change function of jquery ui, but it is not working properly. It is only working when we click out of the textbox. I want action to be triggered when textbox got full value.

I included my code below:

index.php

<html>
<head>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css"
href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />

<script type="text/javascript">
$(document).ready(function(){
$("#name").autocomplete({

source:'search.php',
minLength:1,

change: function (event, ui) { alert("hi"); }

});


});



</script>
</head>

<body>

<form method="post" action="">
Name : <input type="text" id="name" name="name" />
</form>

</body>
</html>


search.php

<?php
$con=mysqli_connect("localhost","root","","datadb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// ...some PHP code for database "my_db"...

// Change database to "test"
mysqli_select_db($con,"datadb");

// ...some PHP code for database "test"...


$term=$_GET["term"];

$query=mysqli_query($con,"SELECT * FROM user where FirstName like '%".$term."%' GROUP by FirstName ");
$json=array();
if (!$query) { // add this check.
die('Invalid query: ' . mysql_error());
}
while($student=mysqli_fetch_array($query)){
$json[]=array(
'value'=> $student["FirstName"],
'label'=>$student["FirstName"]
);
}

echo json_encode($json);

?>

Answer Source

To detect when you select one of your options in the autocomplete you can use the autocompleteselectevent...

$("#name").autocomplete({
    source: 'search.php',
    minLength: 1
}).on('autocompleteselect',function() {
    // Do your stuff here
    alert("hi");
});

I hope it helps

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download