-3

I have created a simple contact form using a bit of PHP code and allowed it to give errors in certain circumstances, such as if a field is empty, or the contents of the field do not match certain criteria. I have applied the standard criteria to the email input box (eg. must contain '@' symbol. But even when all the correct criteria are matched, my PHP still throws and error.

Here is the code:

<?php
if(isset($_POST['email'])){

 // Here is the email to information
 $email_to ="info@example.co.uk";
 $email_subject ="example.co.uk contact form";
 $email_from ="Website Contact Form";
 
 // Error Code
 
 function died($error) {
  echo 'Sorry, there is a problem with the form you submitted. ';
  echo 'These errors appear below.<br/><br/>';
  echo $error. '<br/><br/>';
  echo 'Please go back and fix these errors.';
  die();
  }
  
 // Validation
 
  if(!isset($_POST['name']) ||
  !isset($_POST['email']) ||
  !isset($_POST['comments'])){
   died('All fields must be filled out.');
   } 
  
  $name = $_POST['name'];
  $email = $_POST['email'];
  $comments = $_POST['comments'];
  
  $error_message = "";
  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]
  {2,4}$/';
  if(!preg_match($email_exp, $email)){
   $error_message .= 'The Email Address you entered does not appear to be valid.';
   }
  
  $string_exp = "/^[A-Za-z.'-]+$/";
  if (!preg_match($string_exp, $name)){
   $error_message .= 'The Name you entered does not appear to be valid.';
   }
  if (strlen($comments) < 2 ){
   $error_message .= 'The Comments you entered do not appear to be valid.<br/>';
   }
  if (strlen($error_message) > 0 ){
   died($error_message);
   }
   $email_message = "Form Details Below. \n\n";
   
  function clean_string($string){
   $bad = array("content-type", "bcc:", "to:", "cc:", "href");
   return str_replace($bad, "", $string);
  }
  
  $email_message .= "Name:" . clean_string($name) . "\n";
  $email_message .= "Email:" . clean_string($email) . "\n";
  $email_message .= "Comments:" . clean_string($comments) . "\n";
  
  
  // Create Email Headers
  $headers = 'From: ' .$email_from . "\r\n". 'Reply-To:' . $email. "\r\n" . 
  'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers);
  
?>
<html>
<!-- success message goes here-->
Thank You for contacting us, we will be in touch shortly. <br/>
Please Click <a href="contact.html">here<a/> to go back to the contact page.
</html>
<?php } ?>

Does anyone understand why it is still giving me error messages?

Luke
  • 17
  • 4
  • 1
    What error do you get? – Niet the Dark Absol Nov 07 '16 at 19:19
  • for debugging, kindly remove the `@` from @mail($ema The @ hides errors which may be helpful for debugging – Duane Lortie Nov 07 '16 at 19:20
  • You need show error.. – Olaf Erlandsen Nov 07 '16 at 19:20
  • PS.. the FROM address needs to be valid for your domain,so `$email_from ="Website Contact Form";` is not likely to work. – Duane Lortie Nov 07 '16 at 19:31
  • This is the Error: "Sorry, there is a problem with the form you submitted. These errors appear below. The Email Address you entered does not appear to be valid.The Name you entered does not appear to be valid. Please go back and fix these errors." – Luke Nov 07 '16 at 19:36
  • The error is not a PHP error, it is a catch error that I have created in the code, except it is showing in the wrong instance. So if you type out an email with the correct parameters for example, containing '@' character, it still shows the error message when it shouldn't. – Luke Nov 07 '16 at 19:46
  • @Luke look at my answer. you can use that and don't have any errors. i'm using that too on my website – Blueblazer172 Nov 08 '16 at 13:58

1 Answers1

0

you can use the bootstrap-framework for a nicer look

just like this it will look: bootstrap contact form

your contact.php should contain:

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $human = intval($_POST['human']);
    $from = 'Demo Contact Form';
    $to = "jamiemeechan@outlook.com";
    $subject = 'Email from Tropilac.com';

    $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';
    }
    //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! I 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>';
        }
    }
}
?>

and the html should be:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap Contact Form</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
</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="index.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">
                    <label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
                        <?php echo "<p class='text-danger'>$errHuman</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>

all the source can be found here and here

Blueblazer172
  • 588
  • 2
  • 15
  • 44