Autocomplete putting wrong value in field

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.
  1. $(document).ready(function() {
  2. $("#zlhbcustlastname").autocomplete({
  3. minLength: 3,
  4. source: "ajax/clientlist.php", // Pull in names and addresses
  5. select: function( e, ui ){ // When we select one
  6. var keyvalue = ui.item.value;
  7. $("#zlhbclientinfo").html(hTxt0+'Please Enter Clients Last Name'+aTxt1);

  8. $.post("ajax/readclient.php",{id:keyvalue},
  9. function(data){
  10. if (data.Black_List != 0) { $("#zlhbclientinfo").html(aTxt0+'Alert: Client is Black Listed'+aTxt1); }

  11. $("#zlhbClientID").val(data.Client_ID);
  12. $("#zlhbcustlastname").val(data.Last_Name);
  13. $("#zlhbcustfirstname").val(data.First_Name);
  14. $("#zlhbcellphone").val(data.Phone);
  15. $("#zlhbemail").val(data.Email);
  16. $("#zlhbaddr").val(data.Address1);
  17. $("#zlhbaddr2").val(data.Address2);
  18. $("#zlhbcity").val(data.City);
  19. $("#zlhbstate").val(data.Prov_State);
  20. $("#zlhbzip").val(data.Postal);
  21. $("#zlhbcountry").val(data.Country);

  22. var clname = data.First_Name+" "+data.Last_Name;
  23. $("#paymentCheckName").text(clname);
  24. },"json");
  25. $("#zlhbcommentsbut").button( { disabled: false } );
  26. $("#zlhbcustcommentStatus").val(1);
  27. thgColl(1);
  28. }
  29. });
  30. });
  31. clientlist.php
  32. <?php
  33. $term = trim(strip_tags($_GET['term'])); // Need for the jQuery Autocomplete

  34. // Open the database
  35. include "main.php";
  36. $db = mysql_connect($hostname, $idname, $idpass);
  37. if (!$db) die(mysql_errno($db).": ".mysql_error($db)."\n");

  38. mysql_select_db("treeser");

  39. $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";

  40. $q0 = mysql_query($sql);
  41. if (!$q0){error_log($sql,0);}
  42. else {
  43.     $json = '[';
  44.     $first = true;
  45.     while ($row = mysql_fetch_assoc($q0)) {
  46.         if (!$first) {
  47. $json .=  ',';
  48. } else {
  49. $first = false;
  50. }
  51. $json .= sprintf('{"value":"%s, %s, %s, %s, %s, %s, %d, %s"}',
  52. $row['Last_Name'],$row['First_Name'],$row['Address1'],$row['City'],$row['Prov_State'],$row['Postal'],$row['Client_ID'],$row['Phone']);
  53.     }
  54.     $json .= ']';
  55. }
  56. echo $json;
  57. ?>
  58. readclient.php
  59. <?php
  60. $c = trim(strip_tags($_POST['id']));
  61. $client = explode(',',$c);
  62. $cid = $client[6];

  63. // Open the database
  64. include "main.php";
  65. $db = mysql_connect($hostname, $idname, $idpass);
  66. if (!$db) die("Could NOT connect\n");
  67. mysql_select_db("treeser");

  68. $flds = "Client_ID,Last_Name,First_Name,Address1,Address2,City,Postal,Email,Phone,Phone2,Prov_State,Black_List,Country";
  69. $sql = sprintf("SELECT %s FROM clients WHERE Client_ID = %d",$flds,$cid);

  70. $q0 = mysql_query($sql);
  71. if (!$q0){error_log($sql . ', ' . mysql_errno($db).": ".mysql_error($db),0);}
  72. else {$row = mysql_fetch_array($q0);}
  73. //error_log($row['Last_Name'],0);
  74. echo json_encode($row);
  75. ?>
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.
    • Topic Participants

    • shon