我将PHP验证和PHPmailer结合在一起,代码运行正常。 我现在面临的唯一问题是,每当我单击“提交”时,“ textarea”消息将为空,因此我必须再次输入整个消息。 例如:如果输入了错误的名称,并且所有其他字段都正确,则当您单击“提交”时,它将在名称字段下显示警告消息,但是该消息将为空,我希望我已经以正确的方式发送了我的消息。 *我使用的是没有作曲家的PHPmailer。
index.php :
<!DOCTYPE html>
<html lang="en">
<head>
<title>Event Axis</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div class="container">
<?php include('contact.php');?>
<form id="contact" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<fieldset>
<input class="form-control" placeholder="Your name" type="text" name="name" value="<?= $name ?>" tabindex="1">
<span class="error"><?= $name_error ?></span>
</fieldset>
<fieldset>
<input class="form-control" placeholder="Your Email Address" type="text"
name="email" value="<?= $email ?>" tabindex="2">
<span class="error"><?= $email_error ?></span>
</fieldset>
<fieldset>
<input class="form-control" placeholder="Your Phone Number, ex: 1122334455" type="text" name="phone" value="<?= $phone ?>" tabindex="3">
<span class="error"><?= $phone_error ?></span>
</fieldset>
<fieldset>
<textarea class="form-control" placeholder="Type your message here" type="text" name="message" value="<?= $message ?>" tabindex="5"></textarea>
<span class="error"><?= $message_error ?></span>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" data-submit="Sending.." value="">Submit</button>
<span class="success"><?= $success; ?></span>
</fieldset>
</form>
</div>
</body>
</html>
contact.php :
use PHPMailer\PHPMailer\PHPMailer; //You shall use the following exact namespaces no matter in whathever directory you upload your phpmailer files
use PHPMailer\PHPMailer\Exception;
// define variables and set to empty values
$name_error = $email_error = $phone_error = $message_error = "";
$name = $email = $phone = $message = $success = "";
if($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST["name"])) {
$name_error = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if(!preg_match("/^[a-zA-Z ]*$/", $name)) {
$name_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["phone"])) {
$phone_error = "Phone is required";
} else {
$phone = test_input($_POST["phone"]);
// check if e-mail address is well-formed
if(!preg_match("/^[0-9]{1,10}$/", $phone)) {
//if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)){
$phone_error = "Invalid phone number";
}
}
if(empty($_POST["message"])) {
$message_error = "Message Can Not Be Empty";
} else {
$message = ($_POST["message"]);
//$message = test_input($_POST["message"]);
//$message = "$message";
}
//if all the errors are empty, only then send the message
if($name_error == '' and $email_error == '' and $phone_error == '' and $message_error == '') {
$message_body = '';
unset($_POST['submit']);
foreach($_POST as $key => $value) {
$message_body .= "$key: $value\n";
}
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.ogero.gov.lb'; // Specify main and backup SMTP servers
//$mail->SMTPAuth = true; // Enable SMTP authentication
//$mail->Username = 'user@example.com'; // SMTP username
//$mail->Password = 'secret'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('mozes_86@hotmail.com', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
//echo 'Message has been sent';
$success = "Message sent, thank you for contacting us!";
//reset form values to empty strings
$name = $email = $phone = $message = '';
}
catch(Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
答案 0 :(得分:0)
与input
不同,textarea
标签没有value
属性,其文本直接写在开始和结束标签之间:
<textarea class="form-control" placeholder="Type your message here" type="text" name="message" tabindex="5"><?= $message ?></textarea>
答案 1 :(得分:0)
您需要使用echo
在文本区域中显示值。
然后,请勿使用value=" "
,而应在textarea标签之间使用textarea的值