我正在使用PHP中的联系表单。我对PHP的了解几乎不存在。
我已经尝试了一段时间来获取HTML表单以提交到PHP表单以使其文本字段经过验证并且如果空白但仍然无法正常工作。我也不知道AJAX,否则,会尝试这样做。
所以,已经在HTML页面中使用了PHP自编。
这是当前版本:
<?php
// define variables and set to empty values
$firstNameErr = $lastNameErr = $emailErr = $messageErr = "";
$first_name = $last_name = $email = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["first-name"])) {
$firstNameErr = "First name is required";
} else {
$first_name = test_input($_POST["first-name"]);
}
if (empty($_POST["last-name"])) {
$lastNameErr = "Last name is required";
} else {
$last_name = test_input($_POST["last-name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["message"])) {
$messageErr = "Message is required";
} else {
$message = test_input($_POST["message"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
这是同一页面上的表格:
<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">
<span class="error">* <?php echo $firstNameErr;?></span>
</div>
<div class="field">
<label>Last Name</label>
<input name="last-name" id="last-name" placeholder="Last Name" type="text">
<span class="error">* <?php echo $lastNameErr;?></span>
</div>
<div class="field">
<label>Email</label>
<input name="email" id="email" placeholder="Email" type="email">
<span class="error">* <?php echo $emailErr;?></span>
</div>
<div class="field">
<label>Message</label>
<textarea rows="2" placeholder="Please type in your message" name="message" id="message"></textarea>
<span class="error">* <?php echo $messageErr;?></span>
</div>
<button class="ui button" type="submit">Submit</button>
</form>
有没有办法在两个不同的页面中执行此操作,即第一页中的表单,并以会话模式转移到第二页?或者这是否适用于商业网站?请告诉我。谢谢。
附录
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = filter_vars(test_input($_POST["email"], FILTER_VALIDATE_EMAIL));
}
附录2
我把这个包含在php的底部,即在函数test_input之后。header('location: php_mailer_form.php');
这是正确的位置吗?
但是出于某种原因,当我尝试访问contact.php表单时,它甚至不显示,它只是直接转到我在php_mailer_form.php底部的错误表单。
if(!$mail->send()) {
header('location: url/contactError.html');
} else {
header('location: url/contactResult.html');
}
为什么呢? (如果我需要提供更多信息,请告诉我)。
附录3
<?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');
}
附录4
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact</title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style type="text/css">
.ui.fixed.sticky + p {
margin-top: 39px;
}
.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: contact9Sessions.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>
答案 0 :(得分:2)
这是一个基本表单发送示例。这一切都可以在一页上。在所有html加载到浏览器之前的PHP顶部,{h}页面的<form>
中的任何位置<body>
html:
修改强> 我已更新代码以包含您的PHP Emailer。
<?php
class EmailEngine
{
public static function Send($settings = false)
{
$email = (!empty($settings['email']))? $settings['email']:false;
$first_name = (!empty($settings['first-name']))? $settings['first-name']:false;
$last_name = (!empty($settings['last-name']))? $settings['last-name']:false;
$message = (!empty($settings['message']))? $settings['message']:false;
$alt_message = (!empty($settings['alt_message']))? $settings['alt_message']:'To view the message, please use an HTML compatible email viewer!';
require(__DIR__.'/PHPMailerAutoload.php');
$mail = new PHPMailer;
$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 = $alt_message;
return $mail->send();
}
}
// Just make an error reporting function to return errors
function error_report($val = false)
{
$array["first-name"] = "First name is required";
$array["last-name"] = "Last name is required";
$array["email"] = "Email is required";
$array["message"] = "A message is required";
return (isset($array[$val]))? $array[$val] :false;
}
// Sanitize. I add false so no error is thrown if not set
function sanitize_vals($data = false)
{
if(empty($data))
return false;
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// If a value is posted (any will do from your form) process the rest of the $_POST
if(isset($_POST["first-name"])) {
// Just loop through the $_POST instead of doing all the manual if/else...
foreach($_POST as $key => $value) {
// This specifically processes your email address
if(($key == 'email')) {
// If not valid email, send to errors
if(!filter_var($value,FILTER_VALIDATE_EMAIL))
$errors[$key] = error_report('email');
else
$payload[$key] = $value;
}
// This processes the rest
else {
$value = sanitize_vals($value);
// Checks for empty
if(empty($value))
$errors[$key] = error_report($key);
else
$payload[$key] = $value;
}
}
// If all is good and no errors set, send the email
if(!isset($errors)) {
// SEND MAIL HERE.
$page = (EmailEngine::Send($payload))? "Result" : "Error";
header("Location: url/contact{$page}.html");
exit;
}
}
?>
<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" value="<?php if(!empty($_POST['first-name'])) echo htmlspecialchars($_POST['first-name'],ENT_QUOTES);?>" />
<span class="error">* <?php if(!empty($errors['first-name'])) echo $errors['first-name'];?></span> </div>
<div class="field">
<label>Last Name</label>
<input name="last-name" id="last-name" placeholder="Last Name" type="text" value="<?php if(!empty($_POST['last-name'])) echo htmlspecialchars($_POST['last-name'],ENT_QUOTES);?>" />
<span class="error">* <?php if(!empty($errors['last-name'])) echo $errors['last-name'];?></span> </div>
<div class="field">
<label>Email</label>
<input name="email" id="email" placeholder="Email" type="email" value="<?php if(!empty($_POST['email'])) echo preg_replace('/[^0-9a-zA-Z\@\.\_\-]/',"",$_POST['email']);?>" />
<span class="error">* <?php if(!empty($errors['email'])) echo $errors['email'];?></span> </div>
<div class="field">
<label>Message</label>
<textarea rows="2" placeholder="Please type in your message" name="message" id="message"><?php if(!empty($_POST['message'])) echo htmlspecialchars($_POST['message'],ENT_QUOTES);?></textarea>
<span class="error">* <?php if(!empty($errors['message'])) echo $errors['message'];?></span> </div>
<button class="ui button" type="submit">Submit</button>
</form>
答案 1 :(得分:1)
<?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;
}
?>
<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>
现在在您的其他页面中,要访问变量,只需调用“session_start();”在我们这里做的页面顶部,然后使用调用“$ _SESSION ['message']”来获取消息的值。这回答了你的问题了吗?另请注意,我编辑了html,以便只有在设置了错误变量时才会打印错误消息div。