Skip to content

Instantly share code, notes, and snippets.

@davidsan
Last active December 16, 2015 07:28
Show Gist options
  • Select an option

  • Save davidsan/5398835 to your computer and use it in GitHub Desktop.

Select an option

Save davidsan/5398835 to your computer and use it in GitHub Desktop.
Autocomplete TER LI355
<html>
<head>
<script type="text/javascript" src="5q1.js"></script>
</head>
<body>
<form>
<fieldset>
<p>
<textarea onkeypress="callAjax(this)"></textarea>
</p>
<p>
<input type="submit" value="go"/>
</p>
<div id="totor">
</div>
</fieldset>
</form>
</body>
</html>
function init(){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
window.onload = function(){
init(); // POUR LA VERSION SANS JQUERY
}
function callAjax(prefix){
/** SANS jQuery **/
/*
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
suggest(xmlhttp.responseText);
// document.getElementById("totor").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","./cgi-bin/toto.pl?prefix="+prefix.value,true);
xmlhttp.send(null);
*/
/** AVEC jQuery **/
$.ajax({
type:"get",
url: "./cgi-bin/toto.pl",
data:"prefix="+prefix.value,
complete: function(data){
suggest(data);
},
error: function(jqxhr, ts, et){
alert('failure');
console.log(jqxhr, ts);
}
});
}
function suggest(resp){
var words=eval(resp);
var html="<select onchange=javascript:refresh(this)>";
for (var i = words.length - 1; i >= 0; i--) {
html+="<option value="+words[i]+">"+words[i]+"</option>";
}
html+="</select>";
document.getElementById("totor").innerHTML=html;
}
function refresh(select){
document.getElementsByTagName('textarea')[0].value=select.value;
}
#!/usr/bin/perl
use CGI qw(:standard);
use Data::Dumper;
my $q = CGI->new;
print $q->header('application/json');
$val = $q->param('prefix');
# $val="voiture";
# @dict = ("toto", "tata", "tutu", "tortue");
open FILE, "/usr/share/dict/words" or die $!;
@dict = map {chomp;$_} <FILE>;
@fres=grep { /[a-z]+/ } @dict;
@res=grep { /^$val/ } @fres;
$"="','";
print "['@res']";
#for (my $i = 0; $i < @res; $i++) {
# print $res[$i];
# $str+=", " unless $i==$res-1;
# print "$str";
#}
# $_=Dumper(\@res);
# s/.*=//;
# s/;$//;
print;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment