12

When I use

<form method="post" enctype="text/plain" action="proc.php"> 

form data can not be sent to proc.php file properly. Why? What is the problem? Why I can't use text/plain encoding with post but I can use it with get method?

Narek
  • 38,779
  • 79
  • 233
  • 389
  • 6
    I am pretty sure you don't need to define enctype, unless you are doing a file upload, then it should be: enctype="multipart/form-data" [link](http://www.w3.org/TR/html4/interact/forms.html#adef-enctype) – rwyland Oct 02 '11 at 18:15
  • 1
    According to w3schools ([link](http://www.w3schools.com/tags/att_form_enctype.asp)), `application/x-www-form-urlencoded` is default. – jakub.g Oct 02 '11 at 18:18
  • My question is why method=“post” and enctype=“text/plain” don't work together? My HTML code does not metter and what I should use does not matter too!!!!!!!!!!!!!!!!!!! – Narek Oct 02 '11 at 18:23
  • What does "can not be sent to proc.php file properly" mean exactly? What does the resulting http post look like? – bzlm Oct 02 '11 at 18:27
  • My html code was not figured in the text of the question, sorry. I have corrected. – Narek Oct 02 '11 at 18:28
  • 1
    @Narek I am assuming the browser doesn't know what to do with it since it isn't a valid token. I bet the browser treats enctype="text/plain" the same as enctype="cheeseburger", it does nothing.... – rwyland Oct 02 '11 at 18:29
  • What's the use case for wanting to do this? What information would you expect the browser to put into the content of content type text/plain? – Alohci Oct 02 '11 at 18:34
  • @Alohci What is the use to use text/plain with get method? – Narek Oct 02 '11 at 18:38
  • 1
    Actually, with `method="post" enctype="text/plain"`, browser sends the data, and PHP stores it in `$HTTP_RAW_POST_DATA`, but it doesn't populate `$_POST`. Anyway, why you insist on having `text/plain`? – jakub.g Oct 02 '11 at 18:41
  • @Narek - Get messages shouldn't have a content body at all. [The HTTP 1.1 spec](https://datatracker.ietf.org/doc/draft-ietf-httpbis-p2-semantics/?include_text=1) section 7.3 says "Bodies on GET requests have no defined semantics. Note that sending a body on a GET request might cause some existing implementations to reject the request." – Alohci Oct 02 '11 at 18:52
  • 2
    I have no idea why the question was closed. It's a good question which already has a very good answer. – Karolis Oct 02 '11 at 19:44

3 Answers3

26

[Revised]

The answer is, because PHP doesn't handle it (and it is not a bug):

https://bugs.php.net/bug.php?id=33741

Valid values for enctype in <form> tag are:

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

The first is the default, the second one you need only when you upload files.

@Alohci provided explanation why PHP doesn't populate $_POST array, but store the value inside a variable $HTTP_RAW_POST_DATA.

Example of what can go wrong with text/plain enctype:

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>

file2.php:

<?php
print($HTTP_RAW_POST_DATA);
?>

Result:

input1=abc
input2=def
input2=ghi

No way to distinguish what is the value of input1 and input2 variables. It can be

  • input1=abc\r\ninput2=def, input2=ghi, as well as
  • input1=abc, input2=def\r\ninput2=ghi

No such problem when using the other two encodings mentioned before.

The difference between GET and POST:

  • in GET, the variables are part of URL and are present in URL as query string, therefore they must be URL-encoded (and they are, even if you write enctype="text/plain" - it just gets ignored by the browser; you can test it using Wireshark to sniff the request packets),
  • when sending POST, the variables are not part of URL, but are sent as the last header in HTTP request (POSTDATA), and you can choose whether you want to send them as text/plain or application/x-www-form-urlencoded, but the second one is the only non-ambiguous solution.
jakub.g
  • 38,512
  • 12
  • 92
  • 130
  • 3
    According to [HTML5](http://dev.w3.org/html5/spec/association-of-controls-and-forms.html#attr-fs-enctype) `text/plain` is a third valid content-type for enctype. The format is described at http://dev.w3.org/html5/spec/association-of-controls-and-forms.html#plain-text-form-data. – Alohci Oct 02 '11 at 19:09
  • a nice table is [here](http://www.w3schools.com/tags/att_form_enctype.asp) (I already posted it) – jakub.g Oct 02 '11 at 19:13
  • 5
    Frankly, I never believe a word w3schools says. – Alohci Oct 02 '11 at 19:15
  • Okay, this one looks more credible http://pseudo-flaw.net/content/web-browsers/form-data-encoding-roundup/ ;) – jakub.g Oct 02 '11 at 22:27
  • 1
    [`$HTTP_RAW_POST_DATA`](https://www.php.net/manual/en/reserved.variables.httprawpostdata.php) is DEPRECATED in PHP 5.6, and REMOVED as of PHP 7.0. – N'Bayramberdiyev Jul 04 '20 at 23:38
12

HTML5 does define how to format form data submitted as text/plain here: https://w3c.github.io/html/sec-forms.html#plain-text-form-data.

At the bottom of that section, it says:

Payloads using the text/plain format are intended to be human readable. They are not reliably interpretable by computer, as the format is ambiguous (for example, there is no way to distinguish a literal newline in a value from the newline at the end of the value).

So it not unreasonable that PHP does not attempt to interpret it and only makes it available in raw form. To me, that seems the correct approach.

Alohci
  • 78,296
  • 16
  • 112
  • 156
1

if you're using enctype="text/plain" for debugging purposes, keep in mind that $_POST only contains associate array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type/enctype in the request. So you won't be able to see anything in the script if you're printing $_POSTin the php script.

Also there was a global variable called $HTTP_RAW_POST_DATA which is DEPRECATED in PHP 5.6, and REMOVED as of PHP 7.0. which you could have used to see raw form data as noted by N'Bayramberdiyev

ajuchacko91
  • 1,559
  • 1
  • 10
  • 4