title: jquery autocomplete function add field
body: <p>I am trying to alter some autocomplete code that was given to me.  The existing code automatically fills in a persons address, after selecting a person.  I want to alter it to automatically fill in the address AND insert a custom field.  Below are the changes I have made to the code to attempt this.  I am looking for a working example of how to "auto fill" multiple items using this code, or a link to a tutorial.</p>
  
  <p>autocomplete code :</p>
  
  <pre><code>$('#clientCompanyName').autocomplete({
      source: function( request, response ) {
          $.ajax({
              url : 'ajax.php',
              dataType: "json",
              method: 'post',
              data: {
                  name_startsWith: request.term,
                  type: 'customerName'
              },
              success: function( data ) {
  
                  if(!data.length){
                      var result = [
                        {
                            label: 'No Client found', 
                            value: ''
                        }
                      ];
                      response(result);
                  }else{
                      response( $.map( data, function( item ) {
                          var code = item.split("|");
                              return {
                                  label: code[1],
                                  value: code[1],
                                  data : item
                              }
                          }));
                      }
                  }
  
  
  
              });
      },
      autoFocus: true,            
      minLength: 1,
      select: function( event, ui ) {
          if( typeof ui.item.data !== "undefined" ){
              var names = ui.item.data.split("|");
              $(this).val(names[1]);
              getClientAddress(names[0]);
              // Added getClientField2(names[0]);
              $('#client_id').val( names[0] );
          }else{
              return false;
          }
  
      }               
  });
  function getClientAddress(id){
  
       $.ajax({
           url: "ajax.php",
           method: 'post', 
           data:{id:id, type:'clientAddress'},
           success: function(result){
              $("#clientAddress").html(result);
          }
       });
  }
  //added function getClientField2(id){
  
       $.ajax({
           url: "ajax.php",
           method: 'post', 
           data:{id:id, type:'clientField2'},
           success: function(result){
              $("#clientField2").html(result);
          }
       });
  }
  </code></pre>
  
  <p>Server Code :</p>
  
  <pre><code>if(isset($_POST['type']) &amp;&amp; $_POST['type'] == 'clientAddress' ){
      $id = $_POST['id'];
      $query = "SELECT id, addressLine1 FROM customers WHERE id =$id";
      $result = mysqli_query($db-&gt;con, $query);
      $data = mysqli_fetch_assoc($result);
      $address = '';
      if(!empty( $data )){
          $address = isset($data['addressLine1'])? $data['addressLine1'].' &amp;#13;' : '';
      }
      mysqli_close($db-&gt;con);
      echo $address;exit;
  }
  if(isset($_POST['type']) &amp;&amp; $_POST['type'] == 'clientField2' ){
      $id = $_POST['id'];
      $query = "SELECT id, field2 FROM customers WHERE id =$id";
      $result = mysqli_query($db-&gt;con, $query);
      $data = mysqli_fetch_assoc($result);
      $field2 = '';
      if(!empty( $data )){
          $field2 = isset($data['field2'])? $data['field2'].' &amp;#13;' : '';
      }
      mysqli_close($db-&gt;con);
      echo $field2;exit;
  }
  </code></pre>