Reputation:
A few days I made with my teacher a autocomplete input!
Now I try to test it again, but it suddenly doesn't work anymore!
My teacher is now gona for a while so I have to try it alone.
But I just don't know how to fix this.(Yes I realy tried it.)
It does give a $_GET
.(So that is not the problem)
The JQuery:
var element = '';
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
function getSearchlocation( ) {
return $(element).data('searchlocation');
}
$( ".tag" ).bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( 'checklistHandler.php', {
term: extractLast( request.term ),
searchlocation: getSearchlocation( )
}, response );
},
search: function() {
var term = extractLast( this.value );
element = this;
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
terms.pop();
terms.push( ui.item.value );
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
The Handler:
if ($_GET['term']) {
if($_GET['searchlocation']=='cursus')
{
$table = 'flexibel';
$column = 'app';
}
elseif($_GET['searchlocation']=='boek')
{
$table = 'boeken';
$column = 'naam';
}
else
{
exit;
}
$search = $_GET['term'];
$query = mysql_query("SELECT ".$column." FROM ".$table." WHERE ".$column." LIKE '%".$search."%'");
$result = array();
$i=0;
while($r = mysql_fetch_assoc($query))
{
$result[] = array('id'=>$i, 'label'=>$r[$column], 'value'=>$r[$column]);
$i++;
}
echo json_encode($result);
}
The Input:
if ($onderdeel['formInputType'] == 'autocomplete')
{
?>
<tr>
<input type="hidden" name="buildingblock[<?=$onderdeel['formBuildingBlockId']?>][formBuildingBlockId]" value="<?=$onderdeel['formBuildingBlockId']?>">
<td class="<?= $class ?>" width="320" style="vertical-align: middle;"><?= $onderdeel['formInputLabel'] ?><?=($onderdeel['formRequired'] == 'on' ? '<img title="Verplicht Veld" alt="Verplicht Veld" style="position: relative; top: -10px; height: 5px; width: 5px;" src="'.IMG.'/asterisk_orange.png">' : '')?></td>
<td class="<?= $class ?>" width="320"><textarea style="min-width: 212px; max-width: 212px;" class="tag" data-searchlocation="<?=$onderdeel['formInputOptions']?>" name="buildingblock[<?=$onderdeel['formBuildingBlockId']?>][content]" <?=($onderdeel['formRequired'] == 'on' ? 'required' : '')?>><?=$onderdeel['formContent']?></textarea></td>
<td class="<?= $class ?>" width="60" style="vertical-align: middle; text-align: center;"><input type="checkbox" name="buildingblock[<?=$onderdeel['formBuildingBlockId']?>][checked]" value="1"<?= ($onderdeel['formContentChecked'] == 1 ? ' checked="checked"' : '') ?> /></td>
<td class="<?= $class ?>" width="60" style="vertical-align: middle; text-align: center;"><a href="checklistHandler.php?delete_vraag=<?=$test?>"></a></td>
</tr>
<?
}
The response op the request is:
[{"id":0,"label":"Excel Basis","value":"Excel Basis"},{"id":1,"label":"Excel Essentials","value":"Excel Essentials"},{"id":2,"label":"Excel Gevorderd","value":"Excel Gevorderd"},{"id":3,"label":"Excel: Analyse en Rapportage","value":"Excel: Analyse en Rapportage"},{"id":4,"label":"Excel: Draaitabellen en Grafieken","value":"Excel: Draaitabellen en Grafieken"},{"id":5,"label":"Excel: Functies en Formules","value":"Excel: Functies en Formules"},{"id":6,"label":"Excel: Koppelingen en Macro\u2019s ","value":"Excel: Koppelingen en Macro\u2019s "},{"id":7,"label":"Flexibele cursussen","value":"Flexibele cursussen"},{"id":8,"label":"Word: Complexe Documenten","value":"Word: Complexe Documenten"}]
I Hope you can help me what the problem is.
Greets,
Mathieu.
Upvotes: 2
Views: 70
Reputation: 875
Seems like your while
loop is faulty.
Should be something like:
$i=0;
while($r = mysql_fetch_assoc($query))
{
$result[] = array(
'id'=>$i,
'label'=>$r['label'],
'value'=>$r['value']
);
$i++;
}
Upvotes: 0
Reputation:
Problem Resolved!
Problem was...
I asked the wrong data from the DB!
From $onderdeel['formInputOption']
To $onderdeel['formInputName']
My Apologies!
Upvotes: 2