Accented characters not displaying in a drop down menu

I am able to successfully display foreign characters inside html input fields using htmlspecialchars. But it doesn’t seem to work when I try using the same value in a drop down menu. Any suggestions on what I’m doing wrong?

$adjustedBrand = htmlspecialchars($brand);

$brandHTML .= "<select name='brand'>";
if ($brand == "Améo") {$sel = "selected";} else {$sel = "";}
$brandHTML .= "<option $sel value='Améo'>$adjustedBrand</option>";
$brandHTML .= "</select>";

Until I can get my website redone, it’s currently using charset=iso-8859-1 instead of UTF-8. But like I said, I am able to deal with foreign characters in input fields, so I’m not sure why this is displaying the little question mark icon in place of the accent character in Améo.

Thanks!

If you use $adjustedBrand to display the text for the option, does it work any better if you use it to output the value as well?

$brandHTML .= "<option $sel value='" . $adjustedBrand . "'>$adjustedBrand</option>";

does it work if you use &eacute; instead of the é ?

If so I’ve had to do something similar and strip out various characters that aren’t recognised in my script

I did

$clean = preg_replace('/\p{Zs}/u', ' ', $dirty);
        $clean = str_ireplace(array('<p>','</p>','‘','&nbsp;','<br>','<br />','<Br>','<div>','</div>',"’",'é','£','&pound;','&rsquo;','&lsquo;'),array('','',"'",'','','','','','',"'",'&eacute;','GBP','GBP','',''),$clean);
        $clean = preg_replace('/(\v|\s)+/', ' ', $clean);

I did it as an array so there are a few things you won’t want to take out but hopefully you get the idea. You could create a function to do it instead if you wanted.

hth

But your code doesn’t show us the most important thing - where does $brand come from and what does it contain? The only reasons for incorrect accented characters could be:

  1. Character set mismatch. We know that Améo displays fine in text input fields but we don’t know what $adjustedBrand (coming from $brand) contains. It is very likely that this variable contains text in a different encoding. Check where $brand comes from and check the character encoding of all your php, html and other files, database, etc. - they all need to be in the same character set.
  2. The select box’s font doesn’t have the all the characters you want to display. I’d say this is unlikely but it’s worth mentioning for completeness sake.

This is a workaround that will work but shouldn’t be necessary if the character set of all data is correct.

$brand is being pulled from the mySQL database. I just verified just now that the particular table that contains the brand column is set to:

Character Set: latin1
Collation: latin1_swedish_ci

I don’t remember how I found this out, but I realized that I need to save Améo in the database as Améo instead of Améo.

After digging deeper into this issue it seems like my PHP code, when saving the user’s preference back to the database, is corrupting the data. If I go into the database and do an update statement to the brand column, it will successfully save it as Améo. And then when I refresh my PHP code, it will indeed show Améo in the drop down menu on the webpage. But once I click the save button, the value in the database is changed to something else:

Am�o

So apparently I am doing something wrong when I update the database via the webpage. Here is the relevant code:

$adjustedBrand = htmlspecialchars($brand);
				
if ($brand == "Améo") {$sel = "selected";} else {$sel = "";}
$brandHTML .= "<option $sel value='Améo'>$adjustedBrand</option>";


if ($brand) {
	$brand = "'" . mysql_real_escape_string($brand) . "'";
} else {
	$brand = "NULL";
}


UPDATE members 	SET brand = $brand WHERE memberID = $memberID;

This code works great for all brands except ones that have accented characters, like Améo.

Thanks for your help!

The most likely reason is wrong database connection character set. Make sure you use mysql_set_charset('latin1'). Also, be ware that (as you probably noticed) it is possible to have a situation where you enter data into the database in a wrong character set (different than the database tables) but the characters still display fine on the web page - this can happen when the database’s character set doesn’t match the one you send the data with - but the data is nevertheless pulled in the correct form as entered. But then the characters will actually be corrupted in the database and as a result some things will not work right, for example sorting and searching. If that is the case then you need to convert the data into the correct character set (for example using iconv) - otherwise you are going to have quite a lot of mess in the future.

You could also have your database in utf8 while the web site is in latin1 (iso) because latin1 is a subset of utf8 - you just need proper character set for the connection. In any case it would be much easier for you for the future to change everything to utf8 right away.

Thank you. You helped me decide that I need to make the UTF-8 upgrade a high priority.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.