0

Here's HTML form:

<form action="spledmailer.php" method="post" enctype="text/plain" class="needs-validation" novalidate>
      <div class="container text-center w-50">
        <input type="text" class="form-control" placeholder="Enter Your Name" name="name" required><br>
        <input type="email" class="form-control" placeholder="Enter Your E-mail" id="email" name="Email" required><br>
        <textarea class="form-control" rows="5" placeholder="Tell Us About Your Request" name="query" required></textarea>
      </div>
      <div class="container" style="margin-top: 3%;">
        <div class="row">
            <div class="col text-center">
                <button type="submit" class="btn btn-outline-dark">Send Query</button>
            </div>
        </div>
      </div>
    </form>

So far I've tried this PHP code but it doesn't seems to be working.

if(isset($_POST['submit'])) {
    $to = "example@gmail.com";
    echo $subject = "Form Tutorial";
    echo $name_field = $_POST['name'];
    echo $email_field = $_POST['Email'];
    echo $message = $_POST['query'];

    $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

    echo "Data has been submitted to $to!";
    mail($to, $subject, $body);
} else {
    echo "blarg!";
}

Currently I am not using any web host. I am using localhost.

nick
  • 77
  • 3
  • 13
  • Are you running a local webserver to process the PHP? You can't test PHP using "file://". – Andrew Apr 28 '20 at 17:06
  • yes i am using a local webserver and since i am new to PHP i don't know how to process PHP. – nick Apr 28 '20 at 17:09
  • 1
    If you have a properly setup WAMP/MAMP/LAMP stack then you should be OK to test on your machine - if you're on Windows consider XAMPP, if you do then the following will help getting it setup to send mail from the localhost https://stackoverflow.com/questions/15965376/how-to-configure-xampp-to-send-mail-from-localhost as well of course as changing the $to in your code to a real email address. – Andrew Apr 28 '20 at 17:13

1 Answers1

0

You are trying to check $_POST['submit'] but your form doesn't contain any element with name submit so $_POST['submit'] will never be set.

You can add the submit name and some value to submit button

<button type="submit" class="btn btn-outline-dark" name="submit" value="1">Send Query</button>

Or you can change your condition to use the field that is actually send by your form

if (isset($_POST['query'])) {
    //...
}
Michal Hynčica
  • 5,038
  • 1
  • 12
  • 24
  • Thanks, it worked but now it shows 'blarg' when I submit the form. I think due to some reasons the if condition is not satisfied and therefore it shows blarg message which is in the else part. – nick Apr 29 '20 at 17:38