0

I've created a website and I have no business working in php. The website is up and running, but the form is not working correctly. Right now it submits to my sform.php file, but just shows a blank page and does not submit to the email in the $to portion (which I replaced with [email protected] for privacy reasons). Here is the form code along with the php. I'm sure you all will know exactly what the issue is right away. Sorry for my ignorance.

Form:

<form class="form-horizontal" role="form" method="post" action="sform.php">
                      <div class="form-group">
                        <label for="name" class="col-lg-4 control-label"></label>
                        <div class="col-lg-10">
                          <input type="name" class="form-control" id="name" placeholder="Your Name">
                        </div>
                      </div>
                      <div class="form-group">
                        <label for="email" class="col-lg-4 control-label"></label>
                        <div class="col-lg-10">
                          <input type="email" class="form-control" id="email" placeholder="Email">
                        </div>
                      </div>
                        <div class="form-group">
                        <label for="phone" class="col-lg-4 control-label"></label>
                        <div class="col-lg-10">
                          <input type="phone" class="form-control" id="phone" placeholder="Phone">
                        </div>
                      </div>

                        <div class="form-group">
                        <label for="company" class="col-lg-4 control-label"></label>
                        <div class="col-lg-10">
                          <input type="company" class="form-control" id="company" placeholder="Company Name">
                        </div>
                      </div>


    <div class="form-group">
    <label for="message" class="col-lg-4 control-label"></label>
    <div class="col-lg-10">
    <textarea rows="3" class="form-control" id="message" placeholder="What can we do for you?"></textarea>
    </div>
    </div>
                      <div class="form-group">
                        <div class="col-lg-10">
                          <button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-ok-sign"></span>  Submit</button>
                        </div>
                      </div>
                   </form><!-- form -->

=========================================================

PHP:

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['comments'];
    $human = intval($_POST['human']);
    $from = 'redacted'; 
    $to = '[email protected]'; 
    $subject = 'redacted ';

    $body = "From: $name\n E-Mail: $email\n Comments: $comments\n";

    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!$_POST['comments']) {
        $errMessage = 'Please enter your message';
    }
    //Check if simple anti-bot test is correct
    if ($human !== 5) {
        $errHuman = 'Your anti-spam is incorrect';
    }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
    $result='<div class="alert alert-success">Thank You! We will be in contact with you shortly.</div>';
} else {
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again.</div>';
}
}
}
?>

==================================================================

Thanks again for the help!

J Hall
  • 1
  • Did you make sure the variables used in the `mail` function are all correct? Perhaps `echo` them to see. – Tom Apr 04 '17 at 13:31
  • How would I echo this out? – J Hall Apr 04 '17 at 13:45
  • @JHall your main big problem is that all your form elements does not have name attribute – Masivuye Cokile Apr 04 '17 at 13:50
  • 1
    @MasivuyeCokile Sorry; you know how things are/can get around here ;-) and how they don't usually don't know what to do with tools out there to help them catch the errors. It's been asked too many times and not researched enough. – Funk Forty Niner Apr 04 '17 at 13:53
  • @Fred-ii- no worries I was making the answer a community wiki so didn't lose anything – Masivuye Cokile Apr 04 '17 at 14:06
  • I would simply have my PHP file be one line of code to see if the variables are being passed correctly. `echo $_POST['name'];` – Tom Apr 04 '17 at 18:22

2 Answers2

2

I think you have missed out name attribute in text box. Your code :

<input type="name" class="form-control" id="name" placeholder="Your Name">

Try this :

<input type="text" class="form-control" id="name" name="name" placeholder="Your Name">

And in all input fields do add name="something".

caramba
  • 21,963
  • 19
  • 86
  • 127
Prashanth Harish
  • 158
  • 1
  • 1
  • 17
-1

Your $err* variables are only set if the POST value is not set, but you test them anyway. They don't exist if the POST values are set. By changing things around a bit, you might approach it like:

if (!$_POST['comments']) {
    $errMessage = 'Please enter your message';
} else {
    $errMessage = false;
    $comments = $_POST['comments'];
}

This may not be the best approach as such, but you can probably get this working and then improve it as your skills progress.

markdwhite
  • 2,360
  • 19
  • 24