PHP Form encoding

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jemt
Forum Newbie
Posts: 6
Joined: Sun Jan 31, 2010 5:53 am

PHP Form encoding

Post 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 :-)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Form encoding

Post 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">
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: PHP Form encoding

Post 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">
Post Reply