Eléphanteau du PHP |
31 Messages
25 mai 2009, 08:41
[Edit du 25/05]
J'ai finalement réussi à bidouiller une solution qui marche. Je la donne ici à l'intention de ceux qui auraient rencontré le même problème.
Voici les fonctions javascript :
<script type="text/javascript">
function autocompleteMe(table,champ)
{
new Ajax.Autocompleter (
champ,
'choix_'+champ,
'choix_form.php',
{method: 'post', parameters: 'table='+table+'&champ='+champ, paramName: champ}
);
}
function autocompleteAll(event)
{
autocompleteMe('livres','titre');
autocompleteMe('livres','auteur');
}
$(document).observe('dom:loaded',autocompleteAll);
</script>
et le fichier unique choix_form.php appelé par la fonction javascript :
<?php
$connexion = mysql_connect("localhost","root","");
mysql_select_db('piratage',$connexion);
$table = $_POST['table'];
$champ = $_POST['champ'];
$content = $_POST[$champ];
$query = mysql_query("SELECT $champ FROM $table") or die(mysql_error());
$p=array();
while ($result = mysql_fetch_assoc($query)) $p[] = $result[$champ];
$i=0;
if ($content != "") {
echo '<ul>';
foreach($p as $item) {
if (substr(strtolower($item),0,strlen($content)) == strtolower(stripslashes($content))) {
echo '<li><a href="#" onclick="return false">'.htmlentities($item).'</a></li>';
if (++$i >= 10) die('<li>...</li></ul>');
}
}
echo '</ul>';
}
?>