Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, I know you are using asp.net and my example is based on PHP and MySQL, but since your problem seem to be about the JSON format autocomplete is expecting, I am posting anyway.</p> <p>Get the latest files from the <a href="http://jqueryui.com/download" rel="nofollow noreferrer">jQuery UI page</a>. The autocomplete need UI Core, UI Widget and UI Position from the base package. You also need the autocomplete widget itself.</p> <p>This example works well for me:</p> <p><strong>The HTML:</strong></p> <pre><code>&lt;div&gt; &lt;input id="cities" /&gt; &lt;/div&gt; </code></pre> <p><strong>The script part:</strong></p> <p>(It sends the search variable to cities.php as cities.php?term=)</p> <pre><code>$(function() { $("#cities").autocomplete({ source: "backend/cities.php", minLength: 2, select: function(event, ui) { // perhaps do something with these? region = ui.item.id; country = ui.item.label; city = ui.item.value; secret = ui.item.secret; } }); }); </code></pre> <p><strong>The PHP in cities.php:</strong></p> <p>(The variables <strong>id</strong>, <strong>label</strong> and <strong>value</strong> need to be used. Label is used to populate the drop down list. Value is entered into the input box when the label value is clicked in the list).</p> <pre><code>// important to set header with charset header('Content-Type: text/html; charset=utf-8'); $term = $_POST["term"]; // some database stuff removed // loop it through and build array $n = 0; while ($row = $q-&gt;fetch()) { $row_array[$n]['id'] = $row['City']; $row_array[$n]['label'] = $row['Country']; $row_array[$n]['value'] = $row['Region']; $row_array[$n]['secret'] = 'blabla'; $n++; } // encode it to json format echo json_encode($row_array); </code></pre> <p><strong>The JSON sent back:</strong></p> <p>(When ?term=new york)</p> <pre><code>[{"id":"New York","label":"United States","value":"New York","secret":"blabla"},{"id":"Minnesota","label":"United States","value":"New York Mills","secret":"blabla"},{"id":"New York","label":"United States","value":"New York Mills","secret":"blabla"}] </code></pre> <p>So, to recap:</p> <ul> <li>By default, it sends the variable "term" with the search word to the backend page.</li> <li>It needs "id", "value" and the "label" variables to be sent back.</li> <li>By default the drop down list is populated with the "label"-values.</li> <li>By default, clicking in the list will fill out the input box with "value".</li> <li>You can put additional variable names and make something with them, but the three above is needed.</li> <li>The JSON syntax need to look as provided above.</li> </ul> <p>I hope it helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload