Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: [ajax][xml] polskie znaki
Forum PHP.pl > Forum > XML, AJAX
calebos
Czesc

Staram sie wyedytowac skrypt na moje potrzeby aby miec dynamiczne select menu tzn po wybraniu pierwszego lista drugiego menu zmienia sie zgodnie z ukladem xml.

Chyba utknalem przy parsowaniu tych danych gdzie sa polskie znaki. W bazie danych uzywam cp1250.
W firefoxie dziala mi wybieranie jesli w selecie nie ma polskiego znaku a w IE dziala jedna z 10 opcji jakie sa i zwraca jakis blad null jest pusty lub nie jest obiektem.
Jak podefiniowac te strony kodowe zeby sie wszystko rozumialo?
Main:
  1. <script src="js/AjaxCode.js"></script>
  2. </head>
  3. <h2>Ajax Country/Area Drop-down List Example</h2>
  4. <br>
  5. Select Country:
  6. <br>
  7. <select name="continentList" id="continentList" onChange="return ContinentListOnChange()">
  8. <?php
  9.  
  10. include 'dbcon.php';
  11.  
  12. $conn = mysql_pconnect($hostname, $username) or trigger_error(mysql_error(),E_USER_ERROR);
  13.  
  14. mysql_select_db($database, $conn);
  15. $query = "SELECT distinct TYP_ZGLOSZENIA from coredump1";
  16. $result = mysql_query($query);
  17. while($row = mysql_fetch_assoc($result))
  18. {
  19. echo '<option value="' . $row['TYP_ZGLOSZENIA'] . '">' . $row['TYP_ZGLOSZENIA'] . '</option>';
  20. }
  21.  
  22. ?>
  23. </select>
  24.  
  25. <br>
  26. <br>
  27.  
  28. Select Area:
  29. <BR>
  30. <select name="countryList" id="countryList" >
  31. </select>


handler:

  1. <?php
  2. $filter = $_GET['filter'];
  3. $xml = '';
  4. $iscountry = 1;
  5. $query = "SELECT DISTINCT TYP_ZGLOSZENIA,PODTYP_ZGLOSZENIA from coredump1 where typ_z
    gloszenia = '$filter' order by TYP_ZGLOSZENIA"
    ;
  6. $result = mysql_query($query);
  7. while($row = mysql_fetch_assoc($result))
  8. { 
  9. if ($iscountry == 1)
  10. {
  11. $xml = $xml . '<continent name="' . $row['TYP_ZGLOSZENIA'] . '">';
  12. }
  13.  
  14. $xml = $xml . '<country id="' . $iscountry . '">' . $row['PODTYP_ZGLOSZENIA'] . '</country>';
  15. $iscountry = $iscountry + 1;
  16. }
  17. $xml = $xml . '</continent>';
  18.  
  19. if ($iscountry == 1)
  20. {
  21. $xml = $xml . '<continent name="none">';
  22. $xml = $xml . '<country id="0">no countries found</country>';
  23. $xml = $xml . '</continent>';
  24. }
  25. ?>


ajax

  1. <?php
  2. / declare a global XMLHTTP Request object
  3. var XmlHttpObj;
  4. // create an instance of XMLHTTPRequest Object, varies with browser type, try for 
    IE first then Mozilla
  5. function CreateXmlHttpObj()
  6. {
  7. // try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
  8. try
  9. {
  10. XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
  11. }
  12. catch(e)
  13. {
  14. try
  15. {
  16. XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
  17. } 
  18. catch(oc)
  19. {
  20. XmlHttpObj = null;
  21. }
  22. }
  23. // if unable to create using IE specific code then try creating for Mozilla (FireFox) 
  24. if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
  25. {
  26. XmlHttpObj = new XMLHttpRequest();
  27. }
  28. }
  29.  
  30. // called from onChange or onClick event of the continent dropdown list
  31. function ContinentListOnChange() 
  32. {
  33. var continentList = document.getElementById("continentList");
  34.  
  35. // get selected continent from dropdown list
  36. var selectedContinent = continentList.options[continentList.selectedIndex].value;
  37.  
  38. // url of page that will send xml data back to client browser
  39. var requestUrl;
  40. // use the following line if using asp
  41. requestUrl = "xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedContinent);
  42. // use the following line if using php
  43. // requestUrl = "xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedContinent);
  44.  
  45. CreateXmlHttpObj();
  46.  
  47. // verify XmlHttpObj variable was successfully initialized
  48. if(XmlHttpObj)
  49. {
  50. // assign the StateChangeHandler function ( defined below in this file)
  51. // to be called when the state of the XmlHttpObj changes
  52. // receiving data back from the server is one such change
  53. XmlHttpObj.onreadystatechange = StateChangeHandler;
  54.  
  55. // define the iteraction with the server -- true for as asynchronous.
  56. XmlHttpObj.open("GET", requestUrl, true);
  57.  
  58. // send request to server, null arg when using "GET"
  59. XmlHttpObj.send(null);
  60. }
  61. }
  62.  
  63.  
  64. // this function called when state of XmlHttpObj changes
  65. // we're interested in the state that indicates data has been
  66. // received from the server
  67. function StateChangeHandler()
  68. {
  69. // state ==4 indicates receiving response data from server is completed
  70. if(XmlHttpObj.readyState == 4)
  71. {
  72. // To make sure valid response is received from the server, 200 means response rec
    eived is OK
  73. if(XmlHttpObj.status == 200)
  74. {
  75. PopulateCountryList(XmlHttpObj.responseXML.documentElement);
  76. }
  77. else
  78. {
  79. alert("problem retrieving data from the server, status code: " + XmlHttpObj.status);
  80. }
  81. }
  82. }
  83.  
  84. // populate the contents of the country dropdown list
  85. function PopulateCountryList(countryNode)
  86. {
  87. var countryList = document.getElementById("countryList");
  88. // clear the country list 
  89. for (var count = countryList.options.length-1; count >-1; count--)
  90. {
  91. countryList.options[count] = null;
  92. }
  93.  
  94. var countryNodes = countryNode.getElementsByTagName('country');
  95. var idValue;
  96. var textValue; 
  97. var optionItem;
  98. // populate the dropdown list with data from the xml doc
  99. for (var count = 0; count < countryNodes.length; count++)
  100. {
  101.  textValue = GetInnerText(countryNodes[count]);
  102. idValue = countryNodes[count].getAttribute("id");
  103. optionItem = new Option( textValue, idValue, false, false);
  104. countryList.options[countryList.length] = optionItem;
  105. }
  106. }
  107.  
  108. // returns the node text value 
  109. function GetInnerText (node)
  110. {
  111.  return (node.textContent || node.innerText || node.text) ;
  112. }
  113. ?>


Prosze o przeniesienie do przedszkola - tam chyba bede mial wieksze szanse na pomoc.
Dziekuje.

Self solved - migracja wszystkiego do utf8
Tubis
Jakie cp1250??exclamation.gif

U¿ywaj utf-8 i nie bêdzie problemów.
calebos
Wlasnie zaoralem to wszystko na UTF i teraz ten skypt dziala.

Mam jeszcze pytanie da sie zrobic z tym skryptem tak zeby 3 selecty reagowaly na siebie wzajemnie ?
tzn 3 nalezy do 2 i pierwszego 2 do 1 i np po wybraniu czegos z 3 selecta filtrowal te poprzednie a pokazywal te ktore naleza do siebie jeden do drugiego w zaleznosci od wybrania 1?

Zakrecilem sie troche z tym xmlem i chyba ta wersja js nie do konca jest 'uniwersalna' do tego co bym chcial.
To jest wersja lo-fi g³ównej zawarto¶ci. Aby zobaczyæ pe³n± wersjê z wiêksz± zawarto¶ci±, obrazkami i formatowaniem proszê kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.