1

I am newer to JSON and jQuery . I need to do a jQuery autocomplete, for this I put:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>


<script>
jQuery(document).ready(function($){
    $('#executives').autocomplete({source:'search_executives.php', minLength:2});
});
</script>

And in HTML :

<div class="rows">
    <div class="labels">Search</div>
    <div class="textboxs" style=" width: 175px;"><input id="executives" name="executive" /></div>
</div>

My search_executives.php is :

<?php
session_start();
    if(empty($_SESSION['userid'])){
        header('Location:index.php');
        exit();
    }
include_once('include/config.php'); 

$qry = "SELECT employee_id,fname,mname,lname FROM personal_details WHERE deg_id = '1'";

$res = $dbo->executeQuery( $qry ) ;

//$results[] = array('label' => $row['name']);

for( $i = 0; $i < count( $res ); $i++ )
{
    $result[] = array( 'label' => $res[$i]['fname'].' '.$res[$i]['mname'].' '.$res[$i]['lname'] , 'value' => $res[$i]['employee_id'] ) ;
}

echo json_encode($result);
?>

Now my problem is -

  1. It outputs all the results, it does not depend on the search string I have entered.
  2. It prints the value ( e.g. 3 ) in the textfield in HTML. I need to print the name of the executives with his employee_id as his value.

NB: 1st Part is solved. I forgot to add the LIKE condition in SQL !

2 Answers 2

1

You'd have to set the value of the input as the label, and then have another field to store the employee_id when an item has been selected:

select: function(e, ui) {
  e.preventDefault();
  this.value = ui.item.label;
  $('#other_field').val(ui.item.value);
}

Here's an example

Sign up to request clarification or add additional context in comments.

Comments

0

You are working with an input text box.

The value attribute of the textbox appears in the front end textbox and you cannot change it. (ie) employee_id in your case.

1 Comment

What I need to do if I want the label to be displayed in text box with value attribute from php as its valu ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.