通过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

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.