Home > Backend Development > PHP Tutorial > Why is `enctype='text/plain'` incompatible with the POST method in HTML forms?

Why is `enctype='text/plain'` incompatible with the POST method in HTML forms?

Barbara Streisand
Release: 2024-12-16 04:37:11
Original
885 people have browsed it

Why is `enctype=

Why is "method="post" enctype="text/plain"" not compatible?

When using the HTML form encoding method "post" with "enctype="text/plain"," form data fails to be delivered to the PHP script. What is the reason behind this issue? Why is text/plain encoding incompatible with post while it is allowed with get?

Explanation

PHP does not support "enctype="text/plain"" for "method="post"; it is not a program bug.

Approved values for "enctype" in a form are:

  • application/x-www-form-urlencoded
  • multipart/form-data

The first option is the default, while the second is essential for file uploads.

PHP does not populate the $_POST array when "enctype="text/plain"" is used; instead, it stores the value in $HTTP_RAW_POST_DATA.

Potential Issues with Text/Plain Encoding

Consider the following scenario:

In file1.php:

<form method="post" enctype="text/plain" action="file2.php">
  <textarea name="input1">abc
input2=def</textarea>
  <input name="input2" value="ghi" />
  <input type="submit">
</form>
Copy after login

In file2.php:

<?php
print($HTTP_RAW_POST_DATA);
?>
Copy after login

The expected result:

input1=abc
input2=def
input2=ghi
Copy after login

However, with text/plain encoding, there is no way to distinguish the values of input1 and input2. The result could be:

  • input1=abcrninput2=def, input2=ghi
  • input1=abc, input2=defrninput2=ghi

The difference between GET and POST is that:

  • GET variables appear in the URL as query strings and must be URL-encoded, even with enctype="text/plain."
  • POST variables are transmitted in the HTTP request's final header (POSTDATA), allowing for encoding as either text/plain or application/x-www-form-urlencoded. However, the latter is preferred to avoid ambiguity.

The above is the detailed content of Why is `enctype='text/plain'` incompatible with the POST method in HTML forms?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template