Processing Web Form Input in Latin1

This section describes how to display a Web form and process form input data in Latin1. header() function must be called to override the HTTP response header 'Content-type' setting to Latin1 in PHP 7.

Our PHP script, Web-Form-Input-Latin1.php, is not 100% working as shown in the previous tutorial. It is able to collect input. But it collects it in UTF-8 encoding, not in Latin1 encoding as I want it.

First, I reviewed the default Latin1 text in the script again. It was entered correctly.

Then I ran the script with PHP-CGI to the HTML document is generates:

C:\herong> \local\php\php-cgi Web-Form-Input-Latin1.php
X-Powered-By: PHP/7.3.0
Content-type: text/html; charset=UTF-8

<html><body>
<form><input type="Text" size="40" maxlength="64" name="Input" 
 value="&#x54;&#xE9;&#x6C;&#xE9;&#x76;&#x69;&#x73;&#x69;&#x6F;&#x6E"/><br/>
 <input type="Submit" name="Submit" value="Submit"/>
</form>
</body></html>

I think I know the root cause of the issue. PHP-CGI.exe in PHP 7 engine automatically added "charset=UTF-8" in the Content-Type HTTP response header line.

Content-type: text/html; charset=UTF-8

This will cause the Web browser to display text and collect form input in UTF-8 encoding. And the input data will be delivered back the Web server and the PHP script in UTF-8. This is why my script receives the text in UTF-8.

I had no problem with PHP 5, because it did not specify any encoding in the HTTP response header:

C:\herong> \local\php-5\php-cgi String-GB18030.php

Content-type: text/html
X-Powered-By: PHP/5.0.4

<html><body>
<form><input type="Text" size="40" maxlength="64" name="Input" 
 value="&#x54;&#xE9;&#x6C;&#xE9;&#x76;&#x69;&#x73;&#x69;&#x6F;&#x6E"/><br/>
 <input type="Submit" name="Submit" value="Submit"/>
</form>
</body></html>

In order to fix the issue, we need to call the header() function to override the default header line very early in the PHP script before outputting text in the HTML document:

<?php 
#- Web-Form-Input-Latin1-Fixed.php
#- Copyright (c) 2005-2019 HerongYang.com, All Rights Reserved.
#
  header('Content-Type: text/html; charset=Latin1');
  print('<html><head>');
  print('<meta http-equiv="Content-Type"'.
    ' content="text/html; charset=Latin1"/>');
  print('</head><body>'."\n");

# Default input text
  $input =
    '&#x54;&#xE9;&#x6C;&#xE9;&#x76;&#x69;&#x73;&#x69;&#x6F;&#x6E';
  $input_hex = '54E96CE9766973696F6E'; 

# Form reply determination
  $reply = isset($_REQUEST["Submit"]);

# Process form input data
  if ($reply) {
    if (isset($_REQUEST["Input"])) {
      $input = $_REQUEST["Input"];
    }
  }

# Display form
  print('<form>');
  print('<input type="Text" size="40" maxlength="64"'
   . ' name="Input" value="'.$input.'"/><br/>');
  print('<input type="Submit" name="Submit" value="Submit"/>');
  print('</form>'."\n");

# Display reply
  if ($reply) {
    print('<pre>'."\n");
    print('You have submitted:'."\n");
    print('  Text = '.$input."\n");
    print('  Text in HEX = '.strtoupper(bin2hex($input))."\n");
    print('  Default HEX = '.$input_hex."\n");
    print('</pre>'."\n");
  } 

  print('</body></html>');
?>

After moving this PHP script file to Apache server document directory, I tested it with Internet Explorer (IE) with this URL: http://localhost/Web-Form-Input-Latin1-Fixed.php. I saw a Web page with a form that has the suggested input text and a submit button.

The suggested Latin1 input characters was displayed correctly, even it was generated by my script as HTML entities.

After clicking the submit button, I saw a returning Web page with the same form and a reply section, which confirmed that the input text were correctly received in Latin1 encoding by the PHP script.

Processing Web Form Input in Latin1
Processing Web Form Input in Latin1

It is interesting to note that the return Web page has a special URL which contains the input text inside the query string. All characters in the input text are ASCII characters except two, which are true Latin1 characters presented as Hex values in the URL.

http://localhost/Web-Form-Input-Latin1.php
  ?Input=T%E9l%E9vision&Submit=Submit

Last update: 2019.

Table of Contents

 About This Book

 PHP Installation on Windows Systems

 Integrating PHP with Apache Web Server

 charset="*" - Encodings on Chinese Web Pages

 Chinese Characters in PHP String Literals

 Multibyte String Functions in UTF-8 Encoding

Input Text Data from Web Forms

 Steps and Components Involved

 Processing Web Form Input in ASCII

 Processing Web Form Input in Latin1 Encoding Error

Processing Web Form Input in Latin1

 Entering Latin1 Characters with Alt Keycodes

 Testing Latin1 Alt Keycodes with IE

 Processing Web Form Input in UTF-8

 Outputting Form Default Input Text in UTF-8

 Testing Alt Keycodes with IE on a UTF-8 Web Page

 Input Chinese Text Data from Web Forms

 MySQL - Installation on Windows

 MySQL - Connecting PHP to Database

 MySQL - Character Set and Encoding

 MySQL - Sending Non-ASCII Text to MySQL

 Retrieving Chinese Text from Database to Web Pages

 Input Chinese Text Data to MySQL Database

 Archived Tutorials

 References

 Full Version in PDF/EPUB