jwhite68 Posted August 7, 2007 Share Posted August 7, 2007 I have a strange issue which occurs on a drop down list - for a country and related district. The district can contain unusual characters and accents. Here is an example of some districts for a particular country: $arr_regions["as"][5] = "Ta\'u"; $arr_regions["as"][6] = "Tutuila"; When my form FIRST loads, and you select American Samoa, it correctly displays its regions. So array element 5 above shows as Ta'u (without the back slash). But very strangely when I reload the form, it displays the text in the district drop down as Ta\'u . ie WITH the back slash. Here is the code for the country and district drop downs: <tr> <td class="right1"><span<?=$hlight1?>>Country</span></td> <td colspan="2" style="width:270px"> <select style="width:250px" name="country" onchange="setOptions(document.forms[<?=$form?>].country.options[document.forms[<?=$form?>].country.selectedIndex].value);"> <option value="" selected="selected">Select country</option> <? foreach ($countries as $key => $value) { if ($key==$country) { echo '<option value="'.$key.'" selected>'.$value.'</option>'; } else { echo '<option value="'.$key.'">'.$value.'</option>'; } } ?> </select> </td> <td class="right1">District</td> <td colspan="2" style="width:270px"> <select style="width:250px" name="district" > <option value="" selected="selected">Select country</option> <? if ($country!="") { foreach ($arr_regions[$country] as $key => $value) { if ($key==$district) { echo '<option value="'.$key.'" selected>'.$value.'</option>'; } else { echo '<option value="'.$key.'">'.$value.'</option>'; } } } ?> </select> </td> </tr> Does anyone have any idea how or why this happens, and what I can do to resolve it? [Note: I obviously have another array called $countries which has all of the countries stored. So $arr_regions works together with the country, and represents the districts for a particular country]. Other than this issue, the drop downs behave perfectly fine, and I dont have any issues other than this presentation issue. Quote Link to comment https://forums.phpfreaks.com/topic/63736-displaying-text-in-drop-downs-with-accent-characters-and-similar/ Share on other sites More sharing options...
dbo Posted August 7, 2007 Share Posted August 7, 2007 I'm guessing it's your javascript that handles the onchange event. Quote Link to comment https://forums.phpfreaks.com/topic/63736-displaying-text-in-drop-downs-with-accent-characters-and-similar/#findComment-317576 Share on other sites More sharing options...
jwhite68 Posted August 7, 2007 Author Share Posted August 7, 2007 Correct. Heres the javascript that gets generated from PHP: echo "<script type=\"text/javascript\"> function setOptions(chosen) { var selbox = document.forms[".$form."].district; selbox.options.length = 0; if (chosen == \"\") { selbox.options[selbox.options.length] = new Option('Select country',''); } "; foreach ($countries as $key => $value) { echo "\n".'if (chosen == "'.$key.'") {'."\n"; for ($i=0;$i<count($arr_regions["$key"]);$i++) { echo "selbox.options[selbox.options.length] = new Option('".$arr_regions["$key"]["$i"]."','".$i."');"."\n"; Quote Link to comment https://forums.phpfreaks.com/topic/63736-displaying-text-in-drop-downs-with-accent-characters-and-similar/#findComment-317579 Share on other sites More sharing options...
jwhite68 Posted August 7, 2007 Author Share Posted August 7, 2007 In case its relevant, when I saved the file storing the districts array $arr_regions (ie the php file) I saved it with Emeditor, and ensured it was saved as UTF8. Quote Link to comment https://forums.phpfreaks.com/topic/63736-displaying-text-in-drop-downs-with-accent-characters-and-similar/#findComment-317592 Share on other sites More sharing options...
jwhite68 Posted August 7, 2007 Author Share Posted August 7, 2007 I guess this must be some quirky difference between processing in Javascript and in regular HTML. Javascript needs the backslash in the string. To resolve the issue, I added the function call stripslashes($value) in the processing for District dropdown. ie replacing $value with stripslashes($value). Bad news (for me) is that I have to change many areas of my code to fix this. Quote Link to comment https://forums.phpfreaks.com/topic/63736-displaying-text-in-drop-downs-with-accent-characters-and-similar/#findComment-317671 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.