php - how to pass hidden id using json in jquery ui autocomplete? -
perhaps duplicate,but can't found solution posted question.i use jquery ui auto complete search box. works fine problem want search using id.example:when user type paris,i try send city_id in mysql search. problem how pass hidden id json?
here code:
<input type="text" name="grno" id="grno" class="input" title="<?php echo $lng['vldgrno'];?>
jquery code:
<script> $(function() { function split( val ) { return val.split( /,\s*/ ); } function extractlast( term ) { return split( term ).pop(); } $( "#grno" ) // don't navigate away field on tab when selecting item .bind( "keydown", function( event ) { if ( event.keycode === $.ui.keycode.tab && $( ).data( "ui-autocomplete" ).menu.active ) { event.preventdefault(); } }) .autocomplete({ source: function( request, response ) { $.getjson( "pages/search.php", { term: extractlast( request.term ) }, response ); }, search: function() { // custom minlength var term = extractlast( this.value ); if ( term.length < 1 ) { return false; } }, focus: function() { // prevent value inserted on focus return false; }, select: function( event, ui ) { var terms = split( this.value ); // remove current input terms.pop(); // add selected item terms.push( ui.item.value ); // add placeholder comma-and-space @ end terms.push( "" ); this.value = terms.join( "," ); alert(data.id); return false; } }); }); </script>
autocomplete.php :
<?php mysql_connect('localhost','root',''); mysql_select_db('school'); $return_arr = array(); $term = trim(strip_tags($_get['term']));//retrieve search term autocomplete sends //create select query $query = "select `grno`,`first_name`,`student_id` `tbl_student` `grno` '%".$term."%' or `first_name` '%".$term."%'"; // run query $result = mysql_query ($query); $array = array(); while($obj = mysql_fetch_array($result)){ $array['name'] = $obj['first_name']."(".$obj['grno'].")"; array_push($return_arr,$obj['first_name']."(".$obj['grno'].")"); } $json = json_encode($return_arr); echo $json; ?>
so.how pass student_id
in autocomplete.php
,like label , value.{label='xyz' value='1'}
in autocomplete.php replace code
array_push($return_arr,array("value"=>$obj['student_id'],"label"=>$obj['first_name']."(".$obj['grno'].")"));
and in script change this
terms.push( ui.item.label ); $( "#stud_id" ).val( ui.item.value );
hope,this find.
Comments
Post a Comment