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

发布于 2024-12-20 02:07:34 字数 3048 浏览 0 评论 0 原文

我正在开发一个双下拉菜单 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;
  }
}
      }
    }

I'm working on a double drop down menu a'la http://http://www.coursesweb.net/ajax/multiple-select-dropdown-list-ajax_t See http://www.hafdal.dk/testing/test.php

I'm working with Icelandic characters - my database has utf8_icelandic_ci encoding and my php files have
"meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" in the header.

I had some problems initially to get the Icelandic characters to display in the first dropdown menu but the charset definition in the 2 php files, solved that problem.

The problem now is that apparently ajax does not recognise the string that is loaded into the dropdown menu - an encoding problem again I suspect.

This is my ajax_select.js file:

Thanks in advance for taking a look at this :-)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴者 2024-12-27 02:07:34

我建议您对所有内容都使用 UTF-8。 UTF-8 可以处理您需要的所有非英语字符,并且由于您的数据库采用 UTF-8,因此没有理由不进行切换。我花了无数的时间试图解决这样的问题。 解决方案始终为 UTF-8

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

如果您提供工作代码,我可以查看它,也许帮助你。

I would recommend you to use UTF-8 for everything. UTF-8 handles all non-English characters you'll need and since your database is in UTF-8, there is not reason not to switch. I've used countless hours trying to fix problems like this. The soulution is always UTF-8.

Here is a few other answers on this topic:

If you provide a working code, I can look over it and maybe help you out.

各自安好 2024-12-27 02:07:34

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

我将 utf8_encode() 添加到每个输出中。 F. 前。

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

我希望这可以帮助其他有类似问题的人。

I managed to figure this out by myself (believe it or not!).

I added utf8_encode() to each of my outputs. F.ex.

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

I hope this helps others with similar problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文