3

perhaps it is duplicate,but i can't found solution so i posted this question.I am use jquery ui for auto complete search box. it works fine but the problem is i want to search using id.example:when user type paris,i try to send city_id in mysql for search. so problem is how to pass hidden id with json?
here the 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 from the field on tab when selecting an item
  .bind( "keydown", function( event ) {
    if ( event.keyCode === $.ui.keyCode.TAB &&
        $( this ).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 the current input
      terms.pop();
      // add the selected item
      terms.push( ui.item.value );
      // add placeholder to get the comma-and-space at the 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 the search term that autocomplete sends
 //create select query
  $query = "select `grno`,`first_name`,`student_id` from `tbl_student` where `grno` like '%".$term."%' or `first_name` like '%".$term."%'";
  // Run the 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 to pass student_id in autocomplete.php,like label and value.{label='xyz' value='1'}

DS9
  • 2,995
  • 4
  • 52
  • 102
  • You're not including your hidden id, or the html code for city_id. A thought however is to add the hidden id value to the json-url such as "pages/search.php?hidden_id=" + ("#hidden_id").val(), and then you can access it from search.php with $_GET['hidden_id'] – Robin Manoli Mar 04 '13 at 12:06
  • no, my problem is,in search.php,i get first_name and grno,that is dispaly in autocomplete list,but how to search using `student_id`?like select box,such contry name is display but search by contry_id. – DS9 Mar 04 '13 at 12:11
  • your question is not very clear! but look at your mysql code, it does not search using student_id: ...where `grno` like '%".$term."%' or `first_name` like '%".$term."%' ---> you can add student id here at the end: or student_id = $some_id – Robin Manoli Mar 04 '13 at 12:32

1 Answers1

2

In autocomplete.php replace this code

array_push($return_arr,array("value"=>$obj['student_id'],"label"=>$obj['first_name']."(".$obj['grno'].")")); 

and in your script change this

 terms.push( ui.item.label );
 $( "#stud_id" ).val( ui.item.value );

hope,this is what you find.

  • but it's display id of item when I select any option from autosuggestion. How can I show label of item but it should pass id of item when I press search? – Ghanshyam Katriya Dec 02 '14 at 05:46