Jquery UI Autocomplete - how to add another value with the item? -
i using autocomplete , fetch items database. here js:
$(".add").autocomplete({ source: function(req, add){ $.getjson("add.php?callback=?", req, function(data) { var suggestions = []; $.each(data, function(i, val){ suggestions.push(val.name); }); add(suggestions); }); }, messages: { noresults: '', results: function() {} } });
i sending array php there 'name' , 'id'. want able know id of selected item when user selects without going database, can't put in source without being displayed, don't want. want have inivisible id every item autocomplete finds , when user selects , submits value, can insert object in database.
here php file:
<?php require('connect.php'); $param = $_get["term"]; $query = mysqli_query($connection, "select * stuff name regexp '^$param'"); ($x = 0, $numrows = $query->num_rows; $x < $numrows; $x++) { $row = mysqli_fetch_assoc($query); $add[$x] = array("name" => $row["name"], "id" => $row["id"]); } $response = $_get["callback"] . "(" . json_encode($add) . ")"; echo $response;
hope makes sense.
you take example link: http://jqueryui.com/autocomplete/#custom-data
$("#txtitem").autocomplete({ select: function (event, ui) { return false; } }).data("ui-autocomplete")._renderitem = function( ul, item ) { return $("<li>") .append( "<a onclick='itemclicked(this)' id='" + item.id + "'>" + item.desc + "</a>" ).appendto( ul ); }; function itemclicked(sender) { alert($(source).attr("id");) }
Comments
Post a Comment