Jump to content

Displaying text in drop downs with accent characters and similar


jwhite68

Recommended Posts

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.

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";

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.