Autocomplete : récupérer un tableau php json_encode
Bonjour à tous,
Qu'est-ce qui cloche dans le code suivant ? (je ne récupère pas l'ID dans le champs hidden "id_prod", alors que le label est ok dans le champ "produit") :
JS :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
$(document).ready(function(){
var ac_config = {
url: "autocomplete.php",
maxItemsToShow: 30,
cacheLength: 1,
width:650,
showResult: function(event, ui){
$("#produit").val(ui.item.label);
$("#id_prod").val(ui.item.value);
},
minLength:1
};
$("#produit").autocomplete(ac_config);
}); |
HTML :
Code:
1 2 3 4 5 6
|
<form id="form1" name="form1" method="post" action="">
<input type="text" name="produit" id="produit" />
<input type="hidden" name="id_prod" id="id_prod" />
<input type="submit" name="submit" value="valider" />
</form> |
PHP
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
$q = $_GET['q'];
$tab = array();
$sql = "SELECT id, nom FROMtable WHERE nom LIKE '".FixQuotes($q)."%' AND prix != '0' AND etat='1' ORDER BY nom ";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) >0)
{
while ($row = mysql_fetch_assoc($res))
{
$tab[] = array("label"=>stripslashes(trim($row['nom'])), "value"=>$row['id']);
}
}
echo json_encode($tab); |
Merci de votre aide !