【发布时间】:2015-10-02 06:58:48
【问题描述】:
试图让这个表单工作。已完整发布代码。
它不起作用,即,如果所有字段都是空白的,它会直接转到php_mailer_form.php 页面中的contactError.html 页面。 (已在底部发布php_mailer_form.php。
如果所有字段都填写了,它就可以工作。
如果为空或部分填写,则不会触发任何错误消息。直接去contactError.html。
我在这里缺少什么?我已经尝试了很多不同的变体来完成这项工作,但迄今为止都没有。
<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>
这里是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');
}
【问题讨论】:
-
出于好奇,您上次问这个问题时是否尝试过我的回答? stackoverflow.com/questions/32853422/…
-
输出 HTML 后不能调用
session_start()。它必须在任何输出之前完成。如果您启用了警告,您会看到警告“标头已发送”。 -
...我闻起来像复制品 ^
-
@Fred-ii- 我认为你可能是对的......像往常一样!
-
@Rasclatt 或者......也许不是。见下文... V V V V V