Multiple shortcuts with jQuery autocomplete

I am trying to implement Autocomplete in my project using JQuery UI, however I would like to have a slight modification.

PHP which is dynamically called:

<?php 
include "../../connections/connect.inc.php";


$return_arr = array();


$search_term = "%".$_GET['term']."%";
$moviesquery = "SELECT * FROM movie where movie_name like :term";
$movieresults = $dbconnection->prepare($artistsquery);
$movieresults->bindValue(":term",$search_term);
$movieresults->execute();

/* Retrieve and store in array the results of the query.*/
while ($row = $movieresults->fetch(PDO::FETCH_ASSOC)) {

$return_arr[] = array('label'=>$row['movie_name'],'value'=>$row['movie_name']); //build an array
//array_push($return_arr,$row_array);
}

/* Toss back results as json encoded array. */
echo json_encode($return_arr);

      

This is what I wrote in my JQuery call:

$(function() {
    $("#movie_name").autocomplete({
       source: "sendmovies.php",
       minLength: 3
    })
});

      

The problem is I want to see more information about each movie in the autocomplete suggestions. Currently, autocomplete works and only shows the title of the movie. I want it to display the country under the title and release year. when the user clicks on it, I want the autocomplete to take only the movie name value. And maybe get the movie id in a hidden field. I am a Jquery noobie, so if anyone could help me I would be very glad.

Thanks everyone.

+3


source to share


1 answer


for this part:

And maybe get the movie id in a hidden field. I am a Jquery noobie, so if anyone could help me I would be very glad.

JQuery



$( "#movie_name" ).autocomplete({
    select: function(event, ui){
      if(ui.item.value == ""){
        return false;
      }else{
        //here start your logic (its executed when you select the result
        $('input.hidden').val(ui.item.id);
        $('input[name=any]').val(ui.item.val);
      }
    }
});

      

and you need to add:

/* Retrieve and store in array the results of the query.*/
while ($row = $movieresults->fetch(PDO::FETCH_ASSOC)) {

$return_arr[] = array(
    'label'=>$row['movie_name'], 
    'value'=>$row['movie_name'], 'id' => $row['id'], 
    'anyother_ui_name' => $row['value']); //build an array
//array_push($return_arr,$row_array);
}

      

+1


source







All Articles