Error with PHP Form - Non-functional [closed]

2019-09-12 00:00发布

问题:

Trying to get this form to work. Have posted the code in its entirety.

It doesn't work, ie, if all fields are blank, it goes straight to the contactError.html page in the php_mailer_form.php page. (have posted the php_mailer_form.php at the bottom.

It works if all the fields are filled out.

If empty, or partially filled out, it doesn't trigger any error messages. Just goes straight to the contactError.html.

What am I missing here? I've tried so many different variations of making this work, and none of them to date have.

<html lang="en">
<head>
  <meta charset="utf-8">

  <title> Contact</title>

  <style type="text/css">
      .error {
        color: #FF0000;
      }

  </style>
</head>


<body>

<?php
session_start(); //allows use of session variables

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (!isset($_POST["first-name"])) {
     $firstNameErr = "First name is required";
   } else {
     $first_name = test_input($_POST["first-name"]);
   }

    if (!isset($_POST["last-name"])) {
     $lastNameErr = "Last name is required";
   } else {
     $last_name = test_input($_POST["last-name"]);
   }

   if (!isset($_POST["email"])) {
     $emailErr = "Email is required";
   } else {
     $email = test_input($_POST["email"]);
   }

   if (!isset($_POST["message"])) {
     $messageErr = "Message is required";
   } else {
     $message = test_input($_POST["message"]);
   }

   if(isset($first_name) && isset($last_name) && isset($email) && isset($message))
   {
     $_SESSION['first_name'] = $first_name;
     $_SESSION['last_name'] = $last_name;
     $_SESSION['email'] = $email;
     $_SESSION['message'] = $message;

     header("Location: php_mailer_form.php");
   }
}

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

<div class="ui container">
<div class="ui segment">
  <div>

    <div class="ui fluid five item tabular menu">
      <a class="item" href="index.html">Home</a>
      <a class="item" href="about.html">About</a>
      <a class="item" href="rooms.html">Rooms Info & Rates</a>
      <a class="item" href="book.html">To Book</a>
      <a class="item" href="contact.html">Contact</a>
    </div>

  </div>

<div class="ui two column stackable grid">

<div class="ten wide column">
<form class="ui form"  method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  <div class="field">
    <label>First Name</label>
    <input name="first-name" id="first-name" placeholder="First Name" type="text">
     <?php if(isset($firstNameErr)) print ('<span class="error">* ' . $firstNameErr . '</span>'); ?>
  </div>
  <div class="field">
    <label>Last Name</label>
    <input name="last-name" id="last-name" placeholder="Last Name" type="text">
     <?php if(isset($lastNameErr)) print ('<span class="error">* ' . $lastNameErr . '</span>'); ?>
  </div>
    <div class="field">
    <label>Email</label>
    <input name="email" id="email" placeholder="Email" type="email">
     <?php if(isset($emailErr)) print ('<span class="error">* ' . $emailErr . '</span>'); ?>
  </div>
  <div class="field">
    <label>Message</label>
    <textarea rows="2" placeholder="Please type in your message" name="message" id="message"></textarea>
     <?php if(isset($messageErr)) print ('<span class="error">* ' . $messageErr . '</span>'); ?>
  </div>

  <button class="ui button" type="submit">Submit</button>
</form>
  </div>

  <div class="six wide column">
    <br><br>
    <img class="ui centered large bordered rounded image" src="images/tobereplaced.jpg">
  </div>
</div>

</div>

<div class="ui two column grid">
  <div class="ui left aligned ">
    <p>Left Footer Stuff Here</p>
  </div>

  <div class="ui right aligned">
    <p>Right Footer Stuff Here</p>
  </div>

</div>

</div>
</body>
</html>

Here is the php_mailer_form.php

<?php

session_start();

$first_name = $_SESSION['first-name'];
$last_name = $_SESSION['last-name'];
$email = $_SESSION['email'];
$message = nl2br($_SESSION['message']);

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'host_specified';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'email_specified';                 // SMTP username
$mail->Password = 'password_specified';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; 

$mail->addReplyTo( $email, $first_name );
$mail->addAddress( $email, $first_name );
$mail->addAddress( 'email_specified', 'Staff' );
$mail->From = 'email_specified';
$mail->FromName = 'Staff';


$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Hotel Room Request';
$mail->Body    = $message; 

$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';

if(!$mail->send()) {
    header('location: url/contactError.html');

} else {
    header('location: url/contactResult.html');

}

回答1:

Don't use isset() to test if a form field is filled in. If a form field is empty, it will be set to the empty string when the form is submitted, and if (!isset($_POST['fieldname'])) will not detect it. Use empty() instead:

if (empty($_POST['first-name'])) {
    $firstNameErr = "First name is required";
} else {
    $first_name = test_input($_POST["first-name"]);
}


标签: php html forms