Note that there are some explanatory texts on larger screens.

plurals
  1. POMojolicious::Lite jQuery autocomplete Question
    text
    copied!<p>The autocomplete doesn't work: Is the whole approach wrong or have I made only some errors?</p> <pre><code>#!/usr/local/bin/perl use warnings; use 5.014; use utf8; use Mojolicious::Lite; use DBI; my $dbh = DBI-&gt;connect( ... ) or die $DBI::errstr; my $table = 'my_table'; get '/input' =&gt; sub { my $self = shift; $self-&gt;render( 'input' ); }; get '/search_db' =&gt; sub { my $self = shift; my $col = $self-&gt;param( 'col' ); my $sth = $dbh-&gt;prepare( "SELECT $col FROM $table" ); $sth-&gt;execute(); my $ref; while ( my $row = $sth-&gt;fetchrow_arrayref() ) { push @$ref, @$row; } $self-&gt;render( json =&gt; $ref ); }; app-&gt;start; __DATA__ @@ input.html.ep &lt;!DOCTYPE HTML&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Test&lt;/title&gt; &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8" /&gt; &lt;script src="/js_local/development-bundle/jquery-1.6.2.js"&gt;&lt;/script&gt; &lt;script src="/js_local/development-bundle/ui/jquery.ui.core.js"&gt;&lt;/script&gt; &lt;script src="/js_local/development-bundle/ui/jquery.ui.widget.js"&gt;&lt;/script&gt; &lt;script src="/js_local/development-bundle/ui/jquery.ui.position.js"&gt;&lt;/script&gt; &lt;script src="/js_local/development-bundle/ui/jquery.ui.autocomplete.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(function() { $("#vorname").autocomplete({ source: '/search_db?col=vorname', minLength: 2 }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form&gt; &lt;table&gt; &lt;tr&gt;&lt;td&gt;Vorname:&lt;/td&gt;&lt;td&gt;&lt;input type="text" id="vorname" name="vorname" autocomplete="off"/&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Nachname:&lt;/td&gt;&lt;td&gt;&lt;input type="text" id="nachname" name="nachname" autocomplete="on" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt;&lt;br /&gt; &lt;input type="submit" value="OK"/&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I think I am one step further: now after the second character I get all the names appear as a selection.</p>
 

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