-3

I keep getting the following message when loading the form:


Notice: Undefined index: name in C:\xampp\htdocs\main.php on line 267


Notice: Undefined index: email in C:\xampp\htdocs\main.php on line 275


Notice: Undefined index: message in C:\xampp\htdocs\main.php on line 281

I've looked and followed a number of examples to fix this issue but for some reason I can't seem to find the solution. Any ideas?

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $human = intval($_POST['human']);
    $from = 'enquiry'; 
    $to = 'dgdrd@live.com'; 
    $subject = 'Message from Visitor';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    // 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['message']) {
        $errMessage = 'Please enter your message';
    }

    // 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">We will get in tocuh soon.</div>';
        } else {
            $result='<div class="alert alert-danger">Error! Try again later.</div>';
        }
    }
}
?>

<form class="form-horizontal" role="form" method="post" action="main.php">
    <div class="form-group">
        <label for="name">name</label>
        <input type="text" id="name" name ="name" class="form-control" placeholder="" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                <?php $errName=''; echo "<p class='text-danger'>$errName</p>";?>

    </div>

    <div class="form-group">
        <label for="email1">email</label>
        <input type="email" id="email" name ="email" class="form-control" aria-describedby="emailHelp" placeholder="" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                <?php $errEmail=''; echo "<p class='text-danger'>$errEmail</p>";?>
    </div>

    <div class="form-group">
        <label for="message">comment</label>
        <textarea class="form-control" rows="6" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                <?php $errMessage=''; echo "<p class='text-danger'>$errMessage</p>";?>
    </div>

    <button type="submit" name="submit" id="submit" class="btn btn-primary btn-wrapper">Send</button>

    <hr>

    <div class="form-group">
        <div class="col-sm-12">
            <?php $result=''; echo $result; ?>  
        </div>
    </div>
</form>
Frank M
  • 170
  • 3
  • 14
ffxdean
  • 63
  • 1
  • 4
  • 13
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – jmattheis Jan 14 '17 at 09:57

1 Answers1

2

It basically tell you your $_POST doesn't contain whatever key you getting error. You can give some check before getting value from $_POST

$name = (isset($_POST['name']) && !empty($_POST['name'])) ? $_POST['name'] : "" ;

It basically check wether the $_POST['name'] is empty or not. For extra information use PHPMailer to send mail, if not you will get some trouble such as your mail always send to spam etc.

teck wei
  • 1,375
  • 11
  • 22
  • // Check if name has been entered after this line you already create a $name variable why you still using $_POST['name']? Try to replace it with $name. – teck wei Jan 14 '17 at 10:05
  • Like this: // Check if name has been entered if (!$name) { $errName = 'Please enter your name'; } – ffxdean Jan 14 '17 at 10:10
  • Could you update your html code? I think error is on your html side – teck wei Jan 14 '17 at 10:13
  • No problems. Added. – ffxdean Jan 14 '17 at 10:17
  • Replace your html code with $name as well you did saw the error is in line 267 right? Find out which is the line, I bet is the line in your html code. When you first open up your web page $_POST is surely not being set that is the problem you getting that error. I assume after you submit once your app the error gone. If you really wan to get rid of the error you need create all the variable outside your if(isset($_POST["submit"])) statement as empty varaible $name = ""; Otherwise even you replace your html code with $name it told u undifined variable $name. – teck wei Jan 14 '17 at 10:22
  • @ffxdean after solve your problem you dissapear. Well done bro – teck wei Jan 14 '17 at 11:21
  • No the problem still isn't solved. I tried your solution but I am still getting the same error message. Line 267 is in the HTML code. – ffxdean Jan 14 '17 at 15:00