Processing Chinese Input on Web Forms in GB18030

This section describes how to display a Web form and process form Chinese input data in GB18030.

The next text I did is about Web form input in Chinese using GB18030 encoding, which is a superset of GBK and GB2312. The test PHP script has the following features:

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

# Default input text
  $input = '???';
  $input_hex = 'B5E7CAD3BBFA'; 

# 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('Content-Type:'."\n");
    print('  text/html; charset=gb18030'."\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>');
?>

Note that we need to call header('Content-Type: text/html; charset=GB18030') to override the default encoding setting of UTF-8 generated by the PHP-CGI.exe in PHP 7. This was not needed in older versions of PHP engine, because there was no default encoding setting in the generated HTTP response header.

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-Chinese-GB18030.php. I saw a Web page with a form that has the suggested input text and a submit button.

The default input Chinese characters were displayed correctly.

After clicking the submit button, I saw a returning Web page with the same form and a reply section. The Chinese input characters were received by PHP correctly:

Processing Web Form Chinese Input in GB18030
Processing Web Form Chinese Input in GB18030

It is interesting to note that the return Web page has a special URL which contains the input text inside the query string. The Chinese characters are included as Hex values of GB18030 byte sequences:

http://localhost/Web-Form-Input-Chinese-GB18030.php
  ?Input=%B5%E7%CA%D3%BB%FA&Submit=Submit

Conclusion: IE handles Chinese input text in GB18030 encoding correctly. PHP receives Chinese input text in GB18030 encoding from Web forms correctly.

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

Input Chinese Text Data from Web Forms

 Steps and Components Involved

 Processing Chinese Input on Web Forms in UTF-8

Processing Chinese Input on Web Forms in GB18030

 Processing Chinese Input on Web Forms in Big5

 Copying and Pasting Chinese Input to UTF-8 Web Forms

 Copying and Pasting Chinese Input to GB18030 Web Forms

 Copying and Pasting Chinese Input to Big5 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