0

I'm making a contact form for my website that uses php to send the inputted information to my email.

My issue is that the isset php function returns false. I'm not sure why this is.

Here is my php code (the outputted result is "not sent")

<?php

if (isset($_POST['submit'])){
    $name = $_POST['name'];
    $subject = $_POST['subject'];
    $mailFrom = $_POST['mail'];
    $message = $_POST['message'];

    $mailTo = "[email protected]";
    $headers = "From: " .$mailFrom;
    $txt = "You have received an email from " .$name.".\n\n".$message;

    if (mail($mailTo, $subject, $txt, $headers)){
        echo "<h1>sent successfully</h1>";
    } else {
        echo "<h1>something went wrong</h1>";
    }
} else {
    echo "not sent";
}

?>

Here is my form code

<form action="contactform.php" method="post" enctype="text/plain">
     <label for="name">Name</label>
     <input type="text" name="name" placeholder="Full name.." maxlength="20" required>

     <label for="mail">Email</label>
     <input type="text" name="mail" placeholder="Your email.." 
      pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z{2,}$" required>

     <label for="subject">Subject</label>
     <input type="text" name="subject" maxlength="25" placeholder="Subject..">

     <label for="message">Message</label>
     <input type="text" name="message" placeholder="Message.." style="height:170px" 
      maxlength="150" required>

     <input type="submit" value="submit" name="submit">
</form>
4
  • 2
    What happens when you remove enctype in the form tag? Commented Jul 4, 2020 at 20:20
  • 1
    Just die('<pre>'.print_r($_POST).'</pre>'); ... Commented Jul 4, 2020 at 20:20
  • i think you need to remove enctype , read this (stackoverflow.com/questions/7628249/…) Commented Jul 4, 2020 at 20:27
  • What is making you think that isset returns false? Commented Jul 4, 2020 at 20:30

2 Answers 2

2

There is nothing wrong with isset function. It is not even a PHP-related issue.

It is all about form submission encoding. There are 3 valid encoding types for forms:

  1. application/x-www-form-urlencoded (default)
  2. multipart/form-data
  3. text/plain

You're using the third one, which has documented like:

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).

You're not getting any POST variables since PHP doesn't populate $_POST when using enctype="text/plain".

In the docs of $_POST superglobal:

An associative 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 in the request.


Being said that, you can still read raw data from the request body.

$data = file_get_contents('php://input');

But you'll get the whole data as a string (plain text).

And last but not least, be careful, php://input is not available with enctype="multipart/form-data".

Sign up to request clarification or add additional context in comments.

1 Comment

I changed from text/plain to multipart/form-data and it worked flawlessly. Thanks!!!
1

Don't use isset($_POST['submit']) that's referencing a submit button in case there's more than one submit button on the form as the button is not always pressed. This has caused me issues in the past.

Pressing enter on any field will submit the form with the first submit button available.

Use one of the fields. In your case you can use if (isset($_POST['name'])){

4 Comments

This is not correct. Submitting the form with any mean should send all of its input elements including type=input output.jsbin.com/havison
Okay, I see what you mean Onimusha. I changed the line to if (isset($_POST['name'])). I see what you are saying, but unfortunately it isn't working. The result is still "not sent"
@SaidbakR That's incorrect. A submit button will not be sent if it wasn't clicked i.e. enter was pressed on a text field. Try it. Every other field will be sent. A submit button won't be. See for example stackoverflow.com/questions/22579616/…
@SaidbakR Your jsbin had errors but I tested properly and you are right, the first submit is sent. I posted this answer because I use multiple submit buttons and had this issue. As the first submit button is sent my answer is wrong and I've updated it but now it's unrelated so I'll leave it to moderators to leave or remove.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.