skip to Main Content

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:xampphtdocsmagpieindex1.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 = '[email protected]'; 
    $subject = 'Message from Contact Demo ';

    $body ="From: $namen E-Mail: $emailn 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="[email protected]" 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.

7

Answers


  1. 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) ... )
    
    Login or Signup to reply.
  2. 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>';
        }
    }
        }
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
  5. 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)).

    Login or Signup to reply.
  6. Please find solution below:

        $body ="From: $namen E-Mail: $emailn 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="[email protected]" 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>
    

    Login or Signup to reply.
  7. 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      = '[email protected]';
        $subject = 'Message from Contact Demo ';
    
        $body = "From: $namen E-Mail: $emailn 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="[email protected]" 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search