自动填充功能不再适用

时间:2015-03-17 09:21:03

标签: php jquery ajax json autocomplete

几天后,我和老师一起做了自动填充输入!

现在我尝试再次测试它,但它突然不再起作用了!

我的老师现在已经有一段时间了,所以我必须独自尝试 但我只是不知道如何解决这个问题。(是的,我真的试过了。)
它确实给出$_GET。(所以这不是问题)

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;
 }
});

处理程序:

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);

}

输入:

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>
<?
}

请求的响应是:
[{"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"}]

我希望你能帮我解决问题所在。

映入眼帘,

马修。

2 个答案:

答案 0 :(得分:2)

问题已解决!

问题是......

我向DB询问了错误的数据!

$onderdeel['formInputOption']$onderdeel['formInputName']

我的道歉!

答案 1 :(得分:0)

好像你的while循环有问题。

应该是这样的:

$i=0;
while($r = mysql_fetch_assoc($query))
{
    $result[] = array(
                        'id'=>$i,
                        'label'=>$r['label'],
                        'value'=>$r['value']
                     );
    $i++;
}