doujian1954 2014-07-19 16:53
浏览 57
已采纳

在ajax中使用特殊字符

I'm using the autocompleter of "http://www.devbridge.com/sourcery/components/jquery-autocomplete/" but in a previous version because of its lightweight and speed. the version I used can be found here: https://code.google.com/p/jquery-autocomplete/source/browse/

Within that I try to receive results with

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

$(function() {

$("#ac1").autocomplete('search.php', {
    selectFirst: true
});

$("#flush").click(function() {
    var ac = $("#ac1").data('autocompleter');
    if (ac && $.isFunction(ac.cacheFlush)) {
        ac.cacheFlush();
    } else {
        alert('Error flushing cache');
    }
});

the data.php is very easy structured:

$data = array(
"Berlin" => "10178",
"Hamburg" => "20038",
"München" => "80331",

and the search.php file is the following:

<?php
include 'data.php';
include 'data.php';
function autocomplete_format($results) {
foreach ($results as $result) {
    echo $result[0] . '|' . $result[1] . "
";
}
}


if (isset($_GET['q'])) {
$q = strtolower($_GET['q']);
if ($q) {
    foreach ($data as $key => $value) {
        if (strpos(strtolower($key), $q) !== false) {
            $results[] = array($key, $value);
        }
    }
}
}

$output = 'autocomplete';
if (isset($_GET['output'])) {
$output = strtolower($_GET['output']);
}

if ($output === 'json') {
echo json_encode($results);
} else {
echo autocomplete_format($results);
}

Now I got 2 Questions:

  1. As you see, the data.php consists special charakters like "ü" and "ö". these arent shown correctly in the result. could anybody help me and tell how to fix this?

  2. and the second question is how to echo the second part after the city name and the "=>" regarding to the searched city?

Thank you very much for helping

  • 写回答

1条回答 默认 最新

  • douliudong8108 2014-07-19 17:08
    关注

    I think you need to put utf8_decode() for every return values originating from search.php

    This seems to works

    $results[] = array(utf8_decode($key), $value);
    

    Yes, it works but using utf8_decode() will break a string if it's not encoded in UTF-8 and contains chars not defined in ISO 8859 charset.

    After doing some test it appears autocomplete_format() does not set the correct encoding before echoing array values. Json_encode() in the other hand will escape UTF-8 by default, unless you put JSON_UNESCAPED_UNICODE (PHP 5.4+ only) as argument (but proper JSON parser shouldn't have a problem with unicode escape characters).

    Anyway it appears explicitly setting the encoding managed to fix the problem, at least in my limited testing.

    <?php
    include 'data.php';
    include 'data.php';
    function autocomplete_format($results) {
    foreach ($results as $result) {
        echo $result[0] . '|' . $result[1] . "
    ";
    }
    }
    
    
    if (isset($_GET['q'])) {
    $q = strtolower($_GET['q']);
    if ($q) {
        foreach ($data as $key => $value) {
            if (strpos(strtolower($key), $q) !== false) {
    
    
    
                $results[] = array($key, $value);
            }
        }
    }
    }
    
    $output = 'autocomplete';
    if (isset($_GET['output'])) {
    $output = strtolower($_GET['output']);
    }
    
    //set proper MIME and encoding before echoing the return value
    if ($output === 'json') {
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($results, JSON_UNESCAPED_UNICODE);
    } else {
    header('Content-Type: text/plain; charset=utf-8');
    echo autocomplete_format($results);
    ...
    

    As for question #2

    This can be done with jQuery-autocomplete you're already using.

    Say we have two input boxes, #ac1 for city name (the autocompletized input) and #num1 to hold the numeric value:

     <form>
        <input type="text" id="ac1">
        <input type="text" id="num1">
    </form>
    

    Then you need to add onItemSelect option to #ac1 autocomplete definition inside the <head>

    $(function() {
    
    $("#ac1").autocomplete('search.php', {
        selectFirst: true
    }
    
    //when autocomplete entry is selected..
    
    {onItemSelect: function(item) {
    
    //item = city, data = city's numeric value 
    //fill the value of #num1 input box with the numeric value  
    
     $("#num1").val(item.data);
                                                               }},
    );
    
    . . .
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路