Page 1 of 1

PHP Form encoding

Posted: Mon Feb 01, 2010 1:02 pm
by Jemt
Hi.

I'm having some problems with encoding in one of my applications. I'm therefore trying to understand a few aspects of the problem, by working with some small examples.

Code: Select all

 
<?php
header("content-type: text/html;charset=ISO-8859-1");
?>
 
<html>
<head>
    <title>Test</title>
</head>
<body>
 
<form action="encoding.php" method="post">
    <input name="input">
    <input type="submit">
</form>
 
Value entered: <?php echo $_POST["input"]; ?>
 
<br><br>
 
</body>
</html>
 
The code sample above results in a page encoded with ISO-8859-1. I was quite surprised to see, that entering € (euro sign) in the input field resulted in the character being displayed quite nicely on the page, even though it is not part of ISO-8859-1. It also displays other UTF-8 characters just fine.

Why is this? Are PHP forms always sent as UTF-8 ? Even if this is the case, the fact that the character entered is displayed correctly in the HTML page is a surprise - I would expect it to display a question mark.

Please enlighten me - thanks :-)

Re: PHP Form encoding

Posted: Mon Feb 01, 2010 1:10 pm
by AbraCadaver
I don't know the answer, but what happens if you add this in the head of the page:

Code: Select all

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

Re: PHP Form encoding

Posted: Mon Feb 01, 2010 2:02 pm
by flying_circus
The form has another optional attribute accept-charset. Typically I believe the form should inherit the charset from the page though.

Code: Select all

<form action="encoding.php" method="post" accept-charset="ISO-8859-1">