title: JQuery & PHP Lookup Field
body: <p>I have followed a tutorial on how to create a look up field which dynamically searches a table in the MySQL database - the purpose is to process a contact lookup by postcode. I have the search working and the value (on the page) is inserted into the field however I need to get the id of the record selected into the value="idhere" parameter of the input field. I have added both the PHP and the HTML below can anyone take a look and help resolve my problem.</p>
  
  <p>Javascript &amp; CSS</p>
  
  <pre><code>        &lt;script type="text/javascript"&gt;
          function getSuggestions(value) {
              if(value != ""){ 
              $.post("lookup.php", {contactPart:value}, function(data) {
                  $("#suggestions").html(data);
                  runCSS();
              });
              }else{
                  removeSuggestions();
              }
          }
  
          function removeSuggestions() {
              $("#suggestions").html("");
              stopCSS();  
          }
  
          function addText(value) {
              $("#inputContact").val(value);
              $("#inputContact").val() {
                  $(this).val()
              }   
          }
  
          function runCSS() {
              $("#suggestions").css({
                  'border' : 'solid',
                  'border-width' : '1px'
              });
          }
  
          function stopCSS() {
              $("#suggestions").css({
                  'border' : '',
                  'border-width' : ''
              });
          }
          &lt;/script&gt;
  
          &lt;style type="text/css"&gt;     
          #suggestions {
              text-align: left;
              padding-left: 3px;  
          }
  
          #link:hover {
              background-color: #f0f0f0;
              cursor: default;    
          }
          &lt;/style&gt;
  </code></pre>
  
  <p>HTML</p>
  
  <pre><code>&lt;input type="text" name="userA" id="inputContact" onblur="setTimeout('removeSuggestions()', 200)" onkeyup="getSuggestions(this.value);" value="idhere" /&gt;
  &lt;div id="suggestions"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php 
  
  mysql_connect("127.0.0.1", "root", "root")or die(mysql_error());
  mysql_select_db("gEntry")or die(mysql_error());
  
  $contactPart = mysql_real_escape_string(addslashes($_POST['contactPart']));
  $result = mysql_query("SELECT id, title, firstname, lastname, postcode FROM contact WHERE postcode LIKE '%".$contactPart."%'")or die (mysql_error());
  
  while ($row = mysql_fetch_assoc($result)) {
      echo "&lt;div id='link' onClick='addText(\"".$row['id']. ' - ' .$row['title']. ' ' .$row['firstname']. ' '.$row['lastname']."\");'&gt;" . $row['id'] . ' - ' . $row['title'] . ' ' . $row['firstname'] . ' ' . $row['lastname'] . ' - ' . $row['postcode'] .  "&lt;/div&gt;";
  }
  
  ?&gt;
  </code></pre>
  
  <p>I am using standard mysql syntax as mysqli will not work on my server.</p>
  
  <p>Thanks in advance</p>