简体   繁体   中英

Contact Form not showing error messages or sending email

Before I get into it I would like to say that this did send emails from my local server so I have everything set up. After adding this form validation it no longer sends emails or shows errors. It just refreshes the page. I'm new to php coding so I'm sure I just have an if statement in the wrong order or something like that.

HTML

<section id="contact">
            <h1 class="section-header">Contact Us Directly</h1>
            <h4 class="text-center">Have any quesitons not answered in the <span><a href="questions.php">Questions Page</a></span>.</h4>
            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
                <label for="fname">First Name</label>
                <input type="text" id="fname" name="firstname" value="<?php $firstname ?>" placeholder="Your name.." tabindex="1" autofocus>
                <span class="error"><?php $firstname_error ?></span>
                <label for="lname">Last Name</label>
                <input type="text" id="lname" name="lastname" value="<?php $lastname ?>" placeholder="Your last name.." tabindex="2">
                <span class="error"><?php $lastname_error ?></span>
                <label for="email">Email</label>
                <input type="text" id="email" name="email" value="<?php $email ?>" placeholder="Your email.." tabindex="3">
                <span class="error"><?php $email_error ?></span>
                <label for="message">Message</label>
                <textarea id="subject" name="message" value="<?php $message ?>" placeholder="Write something.." style="height:200px" tabindex="4"> </textarea>
                <span class="error"><?php $message_error ?></span>
                <input type="submit" value="Submit" tabindex="5"> 
                <span class="success"><?php $success ?></span>
            </form>
        </section>

PHP

<?php

$firstname_error = $lastname_error = $email_error = $message_error = "";
$firstname = $lastname = $email = $message = $success = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["firstname"])) {
    $firstname_error = "First name is required";
  } else {
    $firstname = test_input($_POST["firstname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
      $firstname_error = "Only letters and white space allowed"; 
    }
  }

    if (empty($_POST["lastname"])) {
    $lastname_error = "Last name is required";
  } else {
    $lastname = test_input($_POST["lastname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
      $lastname_error = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $email_error = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Invalid email format"; 
    }
  }

  if (empty($_POST["message"])) {
    $message = "";
  } else {
    $message = test_input($_POST["message"]);
  }


if ($firstname_error == '' and $lastname_error == '' and $email_error == '' and $message_error == '' ){
      $message_body = '';
      unset($_POST['submit']);
      foreach ($_POST as $key => $value){
          $message_body .=  "$key: $value\n";
      }

      $EmailFrom = localhost;
      $EmailTo = "testrepairmail69@gmail.com";
      $Subject = "New Order From tabletscreenfixer.com";
      if (mail($EmailTo, $Subject, $message)){
          $success = "Message sent, thank you for contacting us!";
          $firstname = $lastname = $email = $message = '';
      }
  }

}



function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

?>

Your Code is begging for some improvements as @R Smith mentioned; nevertheless this version works; i have tested on my pc. 检查图像

 <?php

  $firstname_error = $lastname_error = $email_error = $message_error = "";
  $firstname = $lastname = $email = $message = $success = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["firstname"])) {
    $firstname_error = "First name is required";
  } else {
    $firstname = test_input($_POST["firstname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
        $firstname_error = "Only letters and white space allowed";
    }
}

if (empty($_POST["lastname"])) {
    $lastname_error = "Last name is required";
} else {
    $lastname = test_input($_POST["lastname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
        $lastname_error = "Only letters and white space allowed";
    }
}

if (empty($_POST["email"])) {
    $email_error = "Email is required";
} else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $email_error = "Invalid email format";
    }
}

if (empty($_POST["message"])) {
    $message = "";
} else {
    $message = test_input($_POST["message"]);
}


if ($firstname_error == '' and $lastname_error == '' and $email_error == '' and $message_error == '' ){
    $message_body = '';
    unset($_POST['submit']);
   // var_dump($_POST); exit();
    foreach ($_POST as $key => $value){
        $message_body .=  "$key: $value\n";
    }
    $EmailFrom = "test@gamil.com";
    $EmailTo = "tabletscreenfixer.com";//-> the message will be sent to this address if you have configure mail stuff well
    $Subject = "New Order From tabletscreenfixer.com";
    if (mail($EmailTo, $Subject, $message)){
        $success = "Message sent, thank you for contacting us!";
        $firstname = $lastname = $email = $message = '';
    }else{
        echo "Failure";
    }
 }else{
    echo "Failure 2";
   }
 }
    function test_input($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }
 ?>

<html>
<head>
    <title>Test</title>
</head>
<body>
<section id="contact">
    <h1 class="section-header">Contact Us Directly</h1>
    <h4 class="text-center">Have any quesitons not answered in the <span><a href="questions.php">Questions Page</a></span>.</h4>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
        <label for="fname">First Name</label>
        <input type="text" id="fname" name="firstname" value="<?php echo $firstname ?>" placeholder="Your name.." tabindex="1" autofocus>
        <span class="error"><?php echo $firstname_error ?></span>
        <label for="lname">Last Name</label>
        <input type="text" id="lname" name="lastname" value="<?php echo $lastname ?>" placeholder="Your last name.." tabindex="2">
        <span class="error"><?php echo $lastname_error ?></span>
        <label for="email">Email</label>
        <input type="text" id="email" name="email" value="<?php echo $email ?>" placeholder="Your email.." tabindex="3">
        <span class="error"><?php echo $email_error ?></span>
        <label for="message">Message</label>
        <textarea id="subject" name="message" value="<?php echo $message ?>" placeholder="Write something.." style="height:200px" tabindex="4"> </textarea>
        <span class="error"><?php echo $message_error ?></span>
        <input type="submit" value="Submit" tabindex="5">
        <span class="success"><?php $success ?></span>
    </form>
</section>
</body>
</html>

EDIT: Missing echo in the input value attribute; Hope it helps;

Add some debugging. For example, everywhere you have an error, increment a error counter. Something like this:

if (empty($_POST["email"])) {
  $email_error = "Email is required";
  $errors++;
} else {
  $email = test_input($_POST["email"]);
  // check if e-mail address is well-formed
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $email_error = "Invalid email format"; 
  }
}

Then, at the end, instead of the long if condition:

if ($firstname_error == '' and $lastname_error == '' and $email_error == '' and $message_error == '' ){

You can use something like:

if ($errors == 0){

Which indicates no errors. Then, inside that if, when you try to send the mail, check for failure:

  if (mail($EmailTo, $Subject, $message)){
      $success = "Message sent, thank you for contacting us!";
      $firstname = $lastname = $email = $message = '';
  }
  else {
      echo "The mail command failed!!";
  }

Finally, give yourself some sort of indicator that there were errors, just for your testing phase. You can remove this else (or even add better styling and keep it):

if ($errors == 0){

 // snip - lines of code in here removed to keep this snippet readable. this is your test and send mail logic.

}
else {
  echo "You've got $errors errors!  You need to correct them!";
}

With these changes, you should be able to find your issues quickly. As I said, you can also remove some of this code (like the echo statements) once you've finished your debugging.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM