Autocomplete putting wrong value in field
I have an autocomplete list that uses a string to identify the items that is concatenated data
ex: Last_Name, First_Name, Address, etc
When I select from the list. This is the value that is inserted in the autocomplete while the rest of the form is populated correctly. Here's the code.
- $(document).ready(function() {
- $("#zlhbcustlastname").autocomplete({
- minLength: 3,
- source: "ajax/clientlist.php", // Pull in names and addresses
- select: function( e, ui ){ // When we select one
- var keyvalue = ui.item.value;
- $("#zlhbclientinfo").html(hTxt0+'Please Enter Clients Last Name'+aTxt1);
- $.post("ajax/readclient.php",{id:keyvalue},
- function(data){
- if (data.Black_List != 0) { $("#zlhbclientinfo").html(aTxt0+'Alert: Client is Black Listed'+aTxt1); }
- $("#zlhbClientID").val(data.Client_ID);
- $("#zlhbcustlastname").val(data.Last_Name);
- $("#zlhbcustfirstname").val(data.First_Name);
- $("#zlhbcellphone").val(data.Phone);
- $("#zlhbemail").val(data.Email);
- $("#zlhbaddr").val(data.Address1);
- $("#zlhbaddr2").val(data.Address2);
- $("#zlhbcity").val(data.City);
- $("#zlhbstate").val(data.Prov_State);
- $("#zlhbzip").val(data.Postal);
- $("#zlhbcountry").val(data.Country);
- var clname = data.First_Name+" "+data.Last_Name;
- $("#paymentCheckName").text(clname);
- },"json");
- $("#zlhbcommentsbut").button( { disabled: false } );
- $("#zlhbcustcommentStatus").val(1);
- thgColl(1);
- }
- });
- });
- clientlist.php
- <?php
- $term = trim(strip_tags($_GET['term'])); // Need for the jQuery Autocomplete
- // Open the database
- include "main.php";
- $db = mysql_connect($hostname, $idname, $idpass);
- if (!$db) die(mysql_errno($db).": ".mysql_error($db)."\n");
- mysql_select_db("treeser");
- $sql = "SELECT DISTINCT Last_Name, First_Name, Address1, City, Prov_State, Postal, Client_ID, Phone FROM clients WHERE Last_Name LIKE '".$term."%' ORDER BY Last_Name";
- $q0 = mysql_query($sql);
- if (!$q0){error_log($sql,0);}
- else {
- $json = '[';
- $first = true;
- while ($row = mysql_fetch_assoc($q0)) {
- if (!$first) {
- $json .= ',';
- } else {
- $first = false;
- }
- $json .= sprintf('{"value":"%s, %s, %s, %s, %s, %s, %d, %s"}',
- $row['Last_Name'],$row['First_Name'],$row['Address1'],$row['City'],$row['Prov_State'],$row['Postal'],$row['Client_ID'],$row['Phone']);
- }
- $json .= ']';
- }
- echo $json;
- ?>
- readclient.php
- <?php
- $c = trim(strip_tags($_POST['id']));
- $client = explode(',',$c);
- $cid = $client[6];
- // Open the database
- include "main.php";
- $db = mysql_connect($hostname, $idname, $idpass);
- if (!$db) die("Could NOT connect\n");
- mysql_select_db("treeser");
- $flds = "Client_ID,Last_Name,First_Name,Address1,Address2,City,Postal,Email,Phone,Phone2,Prov_State,Black_List,Country";
- $sql = sprintf("SELECT %s FROM clients WHERE Client_ID = %d",$flds,$cid);
- $q0 = mysql_query($sql);
- if (!$q0){error_log($sql . ', ' . mysql_errno($db).": ".mysql_error($db),0);}
- else {$row = mysql_fetch_array($q0);}
- //error_log($row['Last_Name'],0);
- echo json_encode($row);
- ?>
The data.Last_Name field becomes the initial prompt from the selection list. The code all works except for the first field where the list is populated from.