Tuesday 20 November 2012

jquery autocomplete and get value from mysql onchange

making a form using PHP, MySQL, and jQuery.
Here is my HTML and PHP structure of form:
<form>
    <label>Employee Name</label>
    <input name="empid" type="text" id="search"  <?php  if (!empty($empid))echo "value='$empid'"; ?>>
    <label>Leave Type</label>
    <select name="leavetype" id="leavetype">
        <option value="">--SELECT--</option>
        <?php Loadlookup("id","leavetype","tbl_leavetypes",$leaveid,$d); ?>
    </select>
    <label>Leave Balance</label>
    <input id="autopopulate" value="">
    <input type="submit">
</form>
I am using autocomplete for finding the employee's name. How do I get both the name and ID of employee by autocomplete, and only show the employee's name in input, but the ID in some hidden field?
Here is my autocomplete code in PHP:
require_once "config.php";
$q = strtolower($_GET["q"]);
if (!$q) return;

$sql = "select DISTINCT FullName as FullName from prmember where FullName LIKE '%$q%'";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
    $cname = $rs['FullName'];
    echo "$cname\n";
and this is the jQuery code for autocomplete:
<script type="text/javascript">
$().ready(function() {
    $("#search").autocomplete("search/search.php", {
        width: 260,
        matchContains: true,
        //mustMatch: true,
        //minChars: 0,
        //multiple: true,
        //highlight: false,
        //multipleSeparator: ",",
        selectFirst: true,


    });
});
</script>
After that, take a look at my HTML form code. when I change <select> in my form, then I want to auto populate the value of <input id="autopopulate" value=""> from the database.
........else try this
$('#leavetype').onchange(function(){
var lvType = $(this).val();

    $.get("search.php?q="+lvType, function(data){
       $('#autopopulate').val(data);
    });

 }
);
try the following and check this for more details

1 comment:

Do you think it could be useful for you? Share your thoughts with us!