Dove Devic Dove Devic - 3 months ago 12
HTML Question

HTML | PHP Submitting form not setting $_POST variables

I have a HTML page where I am trying to set up a mail form eventually. I have the following snippet on my HTML form:

<form action="test_handler.php" method="post" enctype="text/plain">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="user-name">Name</label>
<input type="text" class="form-control" name="name" id="user-name" placeholder="Enter your name" required />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="user-email">Email</label>
<input type="email" class="form-control" name="email" id="user-email" placeholder="Enter your email" required />
</div>
</div>
</div>
<div class="form-group">
<label for="user-message">Message</label>
<textarea class="form-control" rows="3" name="user-message" placeholder="Enter your comment, suggestion, or concern" required></textarea>
</div>
<button type="submit" name="submit" value="send" class="btn tf-btn btn-default">Submit</button>
</form>


yet nothing is transferring... I've tried some debugging statements in test_handler.php which follows:

<?php

echo "Something?";
var_dump($_POST);
var_dump($_GET);
echo $_POST['user-message'];


if(isset($_POST['submit'])){

echo $_POST['user-message'];

?>


and I get the following output:

Something?array(0) { } array(0) { }


Any idea why the HTML is erroneous? Thank you for looking/helping!

Answer

It is because PHP does not handle enctype="text/plain" with $_POST.

Valid values for enctype in tag are:

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

See this

But to get the posted data when using enctype="text/plain" you can use $HTTP_RAW_POST_DATA

<?php
    print($HTTP_RAW_POST_DATA);
?>

Form enctype is application/x-www-form-urlencoded by default in which all characters are encoded before sent (spaces are converted to "+" symbols, and special characters are converted to ASCII HEX code)

Comments