duanmei1694 2013-02-24 17:59
浏览 21
已采纳

通过AJAX获取下拉选项,在像æøå这样的字符时不起作用

My name is Dan and i have seen problems with UTF encoding earlier, read posts related to this kind of issues here on stackoverflow, tried the solutions that you guys have posted but i have to say i am stuck.

Ok, what i am making is a set of dropdowns that get populated via AJAX (jQuery) and PHP when tha values are changed.

  1. ALL files are saved as UTF-8
  2. The index.php file where all gets displayed have meta tag charset=utf-8

The PHP scripts reads directories and outputs list options based on folder names:

$fylke = $_GET['fylke'];
foreach (new DirectoryIterator('../pdfs/'.$fylke) as $fileInfo) {
    if($fileInfo->isDot()) continue;
$kommune = $fileInfo->getFilename();

echo "<option name='kommune' value='".$kommune."'>".$kommune."</option>";
}

The Javascript runs functions based on listbox changes:

function getKommune() {
    $.get("get.php",
  {
    fylke: $("#fylke").val()
  },
  function(data){
    $('#kommune').html(data);
    $('#kommune').attr('disabled', false);
  $('#kommune').prepend('<option selected="selected" value="---">---</option>');
  });
}

All folders and filenames with no special characters lists just fine. When it comes to a special character (æøå) the listbox do not update with folders or files.

I must say i am a beginner at coding, help is much appreciated. Here is a link to the non working project: http://www.tbds.no/pdfdl/

Thanks for the great help.

I want to share the script with you if you want it, but implement security! : www.dan-levi.no/playground/pdfdown/files.zip

  • 写回答

1条回答 默认 最新

  • douchen2011 2013-02-24 18:45
    关注

    I think that the basic problem you're having is that your system's filenames aren't in UTF-8, and you're pulling data directly from the filesystem and pushing it to the browser saying "this is UTF-8".

    I think you need three changes:

    First, at the top of get.php and at the top of index.php, do this before you have any output:

    <?php
    // Set utf-8 in the header, not just as a meta tag
    header('Content-Type: text/html; charset=utf-8');
    ?>
    

    If you don't do this, then the browser treats your get.php output as ISO-8859-1, since the scrap of html you spit out with get.php doesn't have a meta tag to tell it otherwise.

    Second, in the bit that you quoted from get.php, you should pass the incoming fylke parameter through utf8_decode and pass anything going back to the browser through utf8_encode:

    $fylke = utf8_decode($_GET['fylke']);
    foreach (new DirectoryIterator('../pdfs/'.basename($fylke)) as $fileInfo) {
        if($fileInfo->isDot()) continue;
        $kommune = utf8_encode($fileInfo->getFilename());
        echo "<option name='kommune' value='".$kommune."'>".$kommune."</option>";
    }
    

    Third, in index.php, apply utf8_encode to filenames before sending them to the browser.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度