Jump to content

Process Form As Utf-8 ?


sircharlo

Recommended Posts

Hey guys ! Got an interesting problem here. A form resides in question.php. Index.php displays top.php, question.php, and bottom.php. Top.php contains this statement:

<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>

So, the form and its contents should be in UTF-8, right ? Anyway, to make sure, i added this to the form tag:

accept-charset="utf-8"

So, when the user clicks on submit, form is processed using question.php (yes, the same file !) and is validated to see if the user entered all of the needed data. If there are no errors, everything works great. I get the email, accents and all. But if, say, there's an empty field, the user is taken back to the form and the empty field is highlighted. Here's the problem: The form is repopulated with what the user had already put in. But if some letters typed in had accents, such as é à è ç, etc., they are scrambled on re-appearance !!! I don't know why...I created a .htaccess that says that all php files are to be read as utf-8, so that shouldn't be the problem...Help ?

Link to comment
Share on other sites

OK, update: i changed the accept-encoding of the form tag to iso-8859-1, and that works... However, i prefer utf-8 !Anyone know why this is happening ?UPDATE2: OK, never mind... The accents survive the validation stage, but when sent to email, or passed to php as variables, i get gobbledygook.So, what's the deal with this ?

Link to comment
Share on other sites

As long as the page is using a different character encoding, UTF-8 characters will appear strange.In the E-mail content-type header you need to add UTF-8 as the character set. You might also want to do that with the HTML page.

<?phpheader("Content-type: text/html;charset=UTF-8");?>

(For the E-mail, the content-type header would go in the $headers parameter of the mail() function.)

Link to comment
Share on other sites

As long as the page is using a different character encoding, UTF-8 characters will appear strange.In the E-mail content-type header you need to add UTF-8 as the character set. You might also want to do that with the HTML page.
<?php header("Content-type: text/html;charset=UTF-8"); ?>

(For the E-mail, the content-type header would go in the $headers parameter of the mail() function.)

That' just it ! My page is in UTF-8 ! Specified both with meta tag and with the header statement you gave. My problem does not lie with the sending of the email. It lies with the validation of form fields.If form fields are correctly filled, email is sent, accents and all, and everything is ship-shape.However, if a user forgot to fill in a field, the script detects this and shows the user what form field needs to be filled. However, the fields the user had already filled in remain populated (which is what i want), however it seems that in repopulating the fields, the char encoding is lost. Try it with this live demo, you'll see !
Link to comment
Share on other sites

You seem to be applying the htmlentities() function to the data that was sent from the form. One other thing that you have to make sure of is that you saved your PHP file as UTF-8 from your text editor as well.

Link to comment
Share on other sites

Hey all ! I fixed my problem ! Yes ! The problem was not with my page encoding, but rather with the php functions handling the strings. The script I used for my form mail uses the form input fields' value as the data (htmlentities). However, the default charset for htmlentities is :drumroll: iso-8859-1. Yes. So, I had to change my code like so:

<input type="text" name="subject" id="subject" value="<?= htmlentities($values['subject'], ENT_QUOTES, "UTF-8") ?>"/>

This fixed the accents issue. However, quotes and apostrophes were now escaped for no reason ! So, i added stripslashes to complete the fix.

<input type="text" name="subject" id="subject" value="<?= stripslashes(htmlentities($values['subject'], ENT_QUOTES, "UTF-8")) ?>"/>

Voilà ! Everything works great now.Thanks for all your help...

Link to comment
Share on other sites

However, quotes and apostrophes were now escaped for no reason ! So, i added stripslashes to complete the fix.
stripshasles() to eliminate apostrophes and quotes?!?! Doesn't sound reasonable to me...Those were escaped because of the second argument of htmlentities(). Instead of using ENT_QUOTES, use ENT_COMPAT, like so:
<input type="text" name="subject" id="subject" value="<?= htmlentities($values['subject'], ENT_COMPAT, "UTF-8") ?>"/>

This is a more stable and secure solution than stripslashes().

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...