编辑:将PHP更改为推荐代码,仍然无法在页面上显示成功/错误。
我正在尝试让这个联系页面正常工作,但是当我点击提交按钮时似乎正在破坏。单击后,它会将我重定向到/contact_form.php并显示空白页面。如果所有表单都已正确填写,则会发送消息。如果其中任何一个错误或未填写,则会显示相同的空白页。
HTML:
<form method="post" action="contact_form.php">
<div class="input-group-lg">
<label for="name">Your Name<i>*</i></label>
<input name="name" type="text" class="form-control" placeholder="What should I call you?">
</div>
<div class="input-group-lg">
<label for="email">Email Address<i>*</i></label>
<input name="email" type="email" class="form-control" placeholder="Email Address">
</div>
<div class="input-group-lg">
<label for="phone">Phone Number</label>
<input name="phone" type="text" class="form-control" placeholder="Phone Number">
</div>
<div class="input-group-lg">
<label for="message">Message<i>*</i></label>
<textarea name="message" class="form-control" placeholder="What's on your mind?"></textarea>
</div>
<div class="input-group-lg">
<label for="human">What is 2 + 2? (Anti-spam)<i>*</i></label>
<input name="human" type="text" class="form-control" placeholder="2 + 2 = ?">
</div>
<div class="text-center">
<?php echo $result; ?>
</div>
<div class="text-center">
<input name="submit" type="submit" value="Send Message" class="btn btn-custom"></input>
</div>
</form>
PHP:
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'From: example';
$to = 'example@gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 4) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
}
else {
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
}
}
?>
答案 0 :(得分:1)
试试这个。我没有测试过,但这应该像魅力一样。如果您仍然遇到错误,请发布。
if (isset ($_POST['name'])){ //RUN ONLY IF name IS SET
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
// Error handling for missing data
if ((!$name) || (!$email) || (!$phone) || (!$message) || ($human !== 4)){
$result = '<div class="alert alert-danger"><i class="glyphicon glyphicon-warning-sign"></i> <strong>ERROR:</strong> You did not submit the following required information:</div>';
if(!$name){
$result .= '<div class="alert alert-danger"><strong>Please enter your name</strong></div>';
}else if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$result .= '<div class="alert alert-danger"><strong>Please enter a valid email address</strong></div>';
}else if(!$phone){
$result .= '<div class="alert alert-danger"><strong>Please enter a phone number</strong></div>';
}else if(!$message){
$result .= '<div class="alert alert-danger"><strong>Please enter your message</strong></div>';
}else if($human !== 4){
$result .= '<div class="alert alert-danger"><strong>Please correct your addition</strong></div>';
}
} else { // Error handling is ended, process form and send email
$from = 'From: example';
$to = 'example@gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
mail($to, $subject, $body, $from);
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} // Close else after duplication checks
} else { // if the form is not posted with variables, place default empty variables so no warnings or errors show
$name = "";
$email = "";
$phone = "";
$message = "";
$human = "";
$result = "";
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
答案 1 :(得分:0)
您的PHP文件中没有print
或echo
语句...
将echo $result
添加到PHP文件的末尾以查看结果
答案 2 :(得分:0)
更改您的成功代码。这将在页面重新加载时打印您的成功或错误消息。
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} else {
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
}
答案 3 :(得分:0)
确定是否设置了变量:
if (!isset($errName) && !isset($errEmail) && !isset($errMessage) && !isset($errHuman)) {
if (mail ($to, $subject, $body, $from)) {
$result='í<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
if(isset($result)){
echo $result;}