2

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;
  }
}
      }
    }
user1088537
  • 181
  • 3
  • 13
  • 1
    you do realize that UTF8 isnt the same as ISO8859-1? – Jan Dragsbaek Dec 08 '11 at 21:06
  • Your JS gives me the following error `Uncaught ReferenceError: ajaxReq is not defined on line 12` – Jan Dragsbaek Dec 08 '11 at 21:08
  • Always use UTF8 if you don't have very good reasons not to. Especially with non-English character encodings. I've spent too many hours on character encoding to know that (Norwegian characters like æøå) – HNygard Dec 08 '11 at 21:46

2 Answers2

2

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.

Community
  • 1
  • 1
HNygard
  • 4,526
  • 6
  • 32
  • 40
0

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.

user1088537
  • 181
  • 3
  • 13
  • 1
    Are you still setting `charset=iso-8859-1` in the header? What if you just changed that to `utf-8`? And make sure `select_list.php` also sets its header properly. – declan Dec 11 '11 at 00:24