动态Ajax内容冰岛字符集问题

时间:2011-12-08 21:03:13

标签: php ajax character-encoding

我正在使用双下拉菜单a'la http://http://www.coursesweb.net/ajax/multiple-select-dropdown-list-ajax_t请参阅http://www.hafdal.dk/testing/test.php

我正在使用冰岛人物 - 我的数据库有utf8_icelandic_ci编码,我的php文件有 “meta http-equiv =”Content-Type“content =”text / html; charset = iso-8859-1“在标题中。

我最初遇到了一些问题,要在第一个下拉菜单中显示冰岛字符,但是2个php文件中的字符集定义解决了这个问题。

现在的问题是,显然ajax无法识别加载到下拉菜单中的字符串 - 我怀疑这是一个编码问题。

这是我的ajax_select.js文件:

提前感谢您看一下: - )

    // Multiple select lists - www.coursesweb.net/ajax/

    // function used to remove the next lists already displayed when it chooses other options
    function removeLists(colid) {
      var z = 0;
      // removes data in elements with the id stored in the "ar_cols" variable
      // starting with the element with the id value passed in colid
      for(var i=1; i<ar_cols.length; i++) {
        if(ar_cols[i]==null) continue;
        if(ar_cols[i]==colid) z = 1;
        if(z==1) document.getElementById(preid+ar_cols[i]).innerHTML = '';
      }
    } 

    // create the XMLHttpRequest object, according browser
    function get_XmlHttp() {
      // create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
      var xmlHttp = null;

      if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); }     // for Forefox, IE7+, Opera, Safari
      else if(window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    }              // IE5 or 6

              return xmlHttp;
    }

    // sends data to a php file, via POST, and displays the received answer
     function ajaxReq(col, wval) 
    {
      removeLists(col);           // removes the already next selects displayed

      // if the value of wval is not '- - -' and '' (the first option)
      if(wval!='- - -' && wval!='') {
        var request =  get_XmlHttp();             // call the function with the      XMLHttpRequest instance
        var php_file = 'select_list.php';     // path and name of the php file

        // create pairs index=value with data that must be sent to server
        var  data_send = 'col='+col+'&wval='+wval;

        request.open("POST", php_file, true);           // set the request

        document.getElementById(preid+col).innerHTML = 'Loadding...';   // display a loading notification

// adds a header to tell the PHP script to recognize the data as is sent via POST
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(data_send);            // calls the send() method with data_send

// Check request status
// If the response is received completely, will be added into the tag with id value of "col"
request.onreadystatechange = function() {
  if (request.readyState==4) {
    document.getElementById(preid+col).innerHTML = request.responseText;
  }
}
      }
    }

2 个答案:

答案 0 :(得分:2)

我建议你使用UTF-8。 UTF-8处理您需要的所有非英文字符,因为您的数据库是UTF-8,所以没有理由不切换。我用了无数个小时试图修复这样的问题。 洗脱液总是UTF-8

以下是关于此主题的其他一些答案:

如果您提供有效的代码,我可以查看它,也许可以帮助您。

答案 1 :(得分:0)

我设法自己解决了这个问题(信不信由你!)。

我在每个输出中添加了utf8_encode()。 F.ex.

echo utf8_encode("<option value=$nt[id]>$nt[name]</option>");

我希望这有助于其他有类似问题的人。