In this case we will search on one value, but set another value in another field. For demostration purposes we will make this field readonly.
We will use the product table and will set two fields - one for the product name and another for the product id.
In order to demostrate the feature - type some word let say "a", after selecting the value type tab or just leave the field. You will see that the product_id is filled with another value.
Note that if you clear the value of the name the value of the product_id will be cleared too.
The file index.php is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My First PHP jqAutocomplete </title> <link rel="stylesheet" type="text/css" media="screen" href="themes/redmond/jquery-ui.custom.css" /> <script src="js/jquery.js" type="text/javascript"></script> <script src="js/jquery-ui-1.8.1.custom.min.js" type="text/javascript"></script> </head> <body> <form action="#"> <label for="product_name">Product: </label> <input id="product_name" /> <input id="product_id" readonly=readonly name="product_id"/ <input type="submit" name="submit" value="Submit" /> </form> <?php include "autocomplete.php";?> </body> </html>
The autocomplete.php file:
The difference here is in the SelectCommend. We have select three fields.
The first field is used to set the value of the product_name, the second field is used for display the value and the
third field is used to set the value of the product_id field.
<?php require_once '../../../jq-config.php'; require_once ABSPATH."php/jqGridPdo.php"; require_once ABSPATH."php/jqUtils.php"; require_once ABSPATH."php/jqAutocomplete.php"; // Connection to the server $conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD); // Tell the db that we use utf-8 $conn->query("SET NAMES utf8"); // create autocomplete instance $ac = new jqAutocomplete($conn); //write the select command $ac->SelectCommand = "SELECT ProductName, ProductName, ProductID FROM products WHERE ProductName LIKE ? ORDER BY ProductName"; // set the source from where to get data. In this case it is the same file. $ac->setSource("autocomplete.php"); // Set a second parameter, which tell the autocomplete to set a value in the // product_id field. // Enjoy $ac->renderAutocomplete("#product_name", "#product_id"); ?>