-5

I am trying to make a contact form for my website in php, but it seems something wrong somewhere, which i am unable to locate. Can you please help me to locate the error in the following code will be of great help and suggest further improvement on this.

Will of great help. Thanks in advance.

<?php
    // Message Vars
    $msg = '';
    $msgClass = '';

    //check for submit
    if (filter_has_var(INPUT_POST, 'submit')) {
        //GET FORM DATA 
        $name = htmlspecialchars($_POST['name']);
        $email = htmlspecialchars($_POST['email']);
        $message = htmlspecialchars($_POST['message']);

        // check required fields
        if (!empty($email) && !empty($name) && !empty($message)) {
            // passed 
            // check email
            if (filter_var($email, FILTER_VALIDATE_EMAIL)  == false) {
                //failed
                $msg = 'Please use a valid email';
                $msgClass = 'alert-danger';
            } else{
                //passed
                // reciepient email 
                $toEmail = 'myemail@gmail.com';
                $subject = 'Contact Request From '.$name;
                $body = '<h2> Contact Request</h2>
                         <h4> Name </h4><p>'.$name.'</p>
                         <h4> Email </h4><p>'.$email.'</p>
                         <h4> Message </h4><p>'.$message.'</p>';

                // email headers 
                $headers = "MIME-Version: 1.0"."\r\n";
                $headers .= "Content-Type: text/html;charset=UTF-8" . "\r\n";

                // additional headers
                $headers .= "From:" .$name. "<".$email.">". "\r\n";

                if (mail($toEmail, $subject, $body, $headers)) {
                    // email sent
                    $msg = ' Your email has been sent';
                    $msgClass = 'alert-success';
                }
            }

            # code...
        } else {
            //failed
            $msg = ' Please fill in all fields';
            $msgClass = 'alert-danger';
            // failed

            # code...
        }

        # code...
    }
?>

<!DOCTYPE html>
<html>
<head>
    <title>Contact Us</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container" >
        <?php if ($msg != ''): ?>
            <div class="alert <?php echo $msgClass; ?> "><?php echo $msg; ?></div>
        <?php endif; ?>

        <form method="post"  action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <div class="contact-form" >
                <h1>Contact Us</h1>
                <div class="txtb" >
                    <label>Name</label>
                    <input type="text"
                           name="name"
                           class="form-control" 
                           value="<?php echo isset($_POST['name']) ? $name : ''; ?>">
                </div>

                <div class="txtb" >
                    <label>Email</label>
                    <input type="text"
                           name="email"
                           class="form-control" 
                           value="<?php echo isset($_POST['email']) ? $email : ''; ?>" >
                </div>

                <div class="txtb" >
                    <label>Message</label>
                    <textarea  name="message" class="form-control"   ><?php echo isset($_POST['message']) ? $message : ''; ?></textarea>
                </div>

                <br>
                <a type="submit" name="submit"  class="btn" >Submit</a>
        </form>
    </div>
</body>
</html>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sachin
  • 125
  • 9

1 Answers1

1

I don't know what type of the error you're seeing but there's a little thing to change

You need to change

 <a type="submit" name="submit"  class="btn" >Submit</a>

to

 <input type="submit">
EchoDino
  • 35
  • 1
  • 10