我的网站有一个基本的4字段PHP表单。我已将其设置为通过姓名,电子邮件,电话和消息的形式向我发送电子邮件。这部分有效。不起作用的是,当人们没有填写任何内容时,没有错误信息。相关,当提交按钮成功向我的电子邮件发送消息时,没有成功消息。请提前告知并谢谢你!
这是我的表单和PHP
的index.php
<form class="form-horizontal" role="form" method="post" action="processes.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="Shoot me an email!" value="">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<input type="phone" class="form-control" id="phone" name="phone" placeholder="Let's talk!" value="">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-default">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
来自processes.php
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = 'Contact Form';
$to = 'mybusiness@gmail.com';
$subject = 'Contact Form';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
// 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';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
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>';
}
}
}
?>
答案 0 :(得分:2)
尝试这一点,它一直对我有用! :)
首先创建一个错误输出函数:
function error_output($errors){
return '<ol><li>' . implode('</li><li>', $errors) . '</li></ol>';
}
然后,在代码内部使用errors[] =
来设置错误。
这样的事情:
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errors[] = 'Please enter your message';
}
如果errors
变量不为空,请将它们输出到页面:
if(!empty($errors)){
echo error_output($errors);
} else {
//execute code
}
它应该使您的代码更清晰,更容易输出错误!
希望这有帮助!