0

Please help in solving this problem, i tried all the resolutions. I am a beginner in php and knows less about fixing bugs. I just copy pasted this code and now I am getting errors of undefined index and variables. like this one- "Notice: Undefined variable: errName in C:\xampp\htdocs\magpie\index1.php on line 27" and so on.... If you have complete working php contact form code then post that too. Thanks in advance..

    <?php
    if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'contact test'; 
    $to = 'example@domain.com'; 
    $subject = 'Message from Contact Demo ';

    $body ="From: $name\n E-Mail: $email\n Message:\n $message";
    // Check if name has been entered
    if (!isset($_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 (!isset($_POST['message'])) {
        $errMessage = 'Please enter your message';
    }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
    $result='<div class="alert alert-success">Thank You! We will be in touch.</div>';
} else {
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }
}
    }
error_reporting (E_ALL ^ E_NOTICE);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
            <h1 class="page-header text-center">Contact Form Example</h1>
            <form class="form-horizontal" role="form" method="post" action="index1.php">
                <div class="form-group">
                    <label for="name" class="col-sm-2 control-label">Name</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                        <?php echo "<p class='text-danger'>$errName</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <label for="email" class="col-sm-2 control-label">Email</label>
                    <div class="col-sm-10">
                        <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                        <?php echo "<p class='text-danger'>$errEmail</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <label for="message" class="col-sm-2 control-label">Message</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                        <?php echo "<p class='text-danger'>$errMessage</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <?php echo $result; ?>  
                    </div>
                </div>
            </form> 
        </div>
    </div>
    </div>   
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>

Please post any working contact form code or if you can find error in the above code, then please post in the answers. A Big thanks in advance... I am a photoshop, coreldraw and illustrator designer, know little coding of dot net but begginer in php.

  • try by removing action attribute of your form.check once. – Alive to die - Anant May 27 '15 at 06:22
  • try !isset(errName) instead of ! – xenish May 27 '15 at 06:23
  • Notices are not errors - they won't crash your program. They inform you of places where you need to take a second look, because you may have a logic error. Look at where you define the $errName variable. There is a case where that variable will not exist. When you get the notice, you fall into that case. You should define the variable with $errName=null, or $errName='' above the if. – VladFr May 27 '15 at 06:25
  • Just remove your action and try to write echo $resultin last tolines. that's it. it will work. – Alive to die - Anant May 27 '15 at 06:29
  • Great solution, it solved my error problem. But the code is not sending email to my email id. – naman goel May 28 '15 at 09:07

7 Answers7

2

The error simply says that you have a variable which you haven't defined. For instance:

//Some code over here
...
...
echo $bla;

Since $bla wasn't defined and has no value - you'll get a notice regarding this matter.

In your HTML form, you're using those $err variables without defining them. On first sight you would say, I defined the mentioned variables, for instance:

$errMessage = 'Please enter your message';

But, take a closer look, you've defined them under a certain condition, for instance:

if (!isset($_POST['message'])) {

So, in case the form hasn't been submitted yet - those variables are not really defined.

Solution 1: Initial Values

At the top of the page, just define all the variables you're about to use and set a default/empty value.

$errBla = '';
$errBla2 = '';

BTW, I would prefer to use an array for the "error" status purpose.

Solution 2: isset The isset function allows you to check if a variable has been defined or not. So instead of having:

<?php echo "<p class='text-danger'>$errMessage</p>";?>

and:

if (!$errName && !$errEmail && !$errMessage) {

You'll have:

<?php echo (isset($errMessage) ? "<p class='text-danger'>$errMessage</p>" : ''; ?>

and:

if ((isset($errName) && !$errName) && (isset($errEmail) && !$errEmail) ... )
Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39
  • Great solution, it solved my error problem. But the code is not sending email to my email id. – naman goel May 28 '15 at 09:07
  • It seems to be a different question, Do you get the success message and no errors but still you don't get any email to the address that's in `$to`? – Ofir Baruch May 28 '15 at 10:19
  • Check out the following: http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-e-mail – Ofir Baruch May 29 '15 at 17:34
0

Your $errMessage is only set if you don't have a message. Same with $errName and $errEmail. To fix that, you want to use isset:

if (!isset($errName) || !isset($errEmail) || !isset($errMessage)) {

I also changed it to or ( || ), as otherwise your script will continue executing no matter if you set an error message. This snippet checks if an error message exists and shows the error if only one is set.

Matthew B
  • 84
  • 8
0

It's not an error, it's just a notice which the variables are not yet set but your main problem is that you're using xampp which you should consider implementing this CLICK this LINK

I believe that you just copy your source code and I believe it's also working so I guess you should enable your email server first from your xampp or test it online servers.

Community
  • 1
  • 1
yamidemichaos
  • 110
  • 1
  • 10
0

To check wether a variable exists in PHP you should use the isset() function. It will return true if the variable is set and not null.

The ! returns the opposite of the boolean variable it precedes.

Instead of if(!$errName) use if(isset($errName)).

0

Please find solution below:

    $body ="From: $name\n E-Mail: $email\n Message:\n $message";
    // Check if name has been entered
    if (!isset($_POST['name'])) {
        $errName = 'Please enter your name';
    } else {
        $errName = '';  
    }

    // 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';
    } else {
        $errEmail = ''; 
    }

    //Check if message has been entered
    if (!isset($_POST['message'])) {
        $errMessage = 'Please enter your message';
    } else {
        $errMessage = '';   
    }

    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You! We will be in touch.</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
            }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
            <h1 class="page-header text-center">Contact Form Example</h1>
            <form class="form-horizontal" role="form" method="post" action="index1.php">
                <div class="form-group">
                    <label for="name" class="col-sm-2 control-label">Name</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                        <?php echo "<p class='text-danger'>$errName</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <label for="email" class="col-sm-2 control-label">Email</label>
                    <div class="col-sm-10">
                        <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                        <?php echo "<p class='text-danger'>$errEmail</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <label for="message" class="col-sm-2 control-label">Message</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                        <?php echo "<p class='text-danger'>$errMessage</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <?php echo $result; ?>  
                    </div>
                </div>
            </form> 
        </div>
    </div>
    </div>   
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>
Super User
  • 9,448
  • 3
  • 31
  • 47
0

Check it i will fix the issues...

  <?php
if (isset($_POST["submit"])) {
    $name    = $_POST['name'];
    $email   = $_POST['email'];
    $message = $_POST['message'];
    $from    = 'contact test';
    $to      = 'example@domain.com';
    $subject = 'Message from Contact Demo ';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
    // Check if name has been entered
    if (!isset($_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 (!isset($_POST['message'])) {
        $errMessage = 'Please enter your message';
    }

    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage) {
        if (mail($to, $subject, $body, $from)) {
            $result = '<div class="alert alert-success">Thank You! We will be in touch.</div>';
        } else {
            $result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
        }
    }
}
error_reporting(E_ALL ^ E_NOTICE);
?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3">
                <h1 class="page-header text-center">Contact Form Example</h1>
                <form class="form-horizontal" role="form" method="post" action="index1.php">
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">Name</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php
echo htmlspecialchars($_POST['name']);
?>">
                            <?php
if (!isset($_POST['name'])) {
    echo "<p class='text-danger'>$errName</p>";
}


?>
                       </div>
                    </div>
                    <div class="form-group">
                        <label for="email" class="col-sm-2 control-label">Email</label>
                        <div class="col-sm-10">
                            <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php
echo htmlspecialchars($_POST['email']);
?>">
                            <?php
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    echo "<p class='text-danger'>$errEmail</p>";
}


?>
                       </div>
                    </div>
                    <div class="form-group">
                        <label for="message" class="col-sm-2 control-label">Message</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" rows="4" name="message"><?php
echo htmlspecialchars($_POST['message']);
?></textarea>
                            <?php
if (!isset($_POST['message'])) {
    echo "<p class='text-danger'>$errMessage</p>";
}

?>
                       </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-10 col-sm-offset-2">
                            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-10 col-sm-offset-2">
                            <?php
echo $result;
?>  
                        </div>
                    </div>
                </form> 
            </div>
        </div>
        </div>   
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    </body>
    </html>
Rahul Saxena
  • 465
  • 4
  • 15
-1

Please change in your if condition. See in below code :

// If there are no errors, send the email

if(!isset($errName) || !isset($errEmail) || !isset($errMessage)) {   // Change this line
if (mail ($to, $subject, $body, $from)) {
    $result='<div class="alert alert-success">Thank You! We will be in touch.</div>';
} else {
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }
}
    }