[英]what is wrong with my contact form, once i click the submit button it keep saying Error occurd! Please try again
<?php
$to = 'marivelcoresis@gmail.com'; // please change this email id
$errors = array();
// print_r($_POST);
// Check if name has been entered
if (!isset($_POST['name'])) {
$errors['name'] = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!isset($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors['email'] = 'Please enter a valid email address';
}
//Check if message has been entered
if (!isset($_POST['message'])) {
$errors['message'] = 'Please enter your message';
}
$errorOutput = '';
if(!empty($errors)){
$errorOutput .= '<div class="alert alert-danger alert-dismissible" role="alert">';
$errorOutput .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>';
$errorOutput .= '<ul>';
foreach ($errors as $key => $value) {
$errorOutput .= '<li>'.$value.'</li>';
}
$errorOutput .= '</ul>';
$errorOutput .= '</div>';
echo $errorOutput;
die();
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $email;
$subject = 'Contact Form : Texas Lawers Responsive HTML5 Template';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
//send the email
$result = '';
if (mail ($to, $subject, $body)) {
$result .= '<div class="alert alert-success alert-dismissible" role="alert">';
$result .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>';
$result .= 'Thank You! I will be in touch';
$result .= '</div>';
echo $result;
die();
}
$result = '';
$result .= '<div class="alert alert-danger alert-dismissible" role="alert">';
$result .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>';
$result .= 'Something bad happend during sending this message. Please try again later';
$result .= '</div>';
echo $result;
die();
?>
乍一看,根据提供的信息,我的建议是改变
$email = $_POST['email'];
和
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
除此之外,最好过滤所有$_POST
变量,因为这些检查不足以清除隐藏在文本中的任何恶意代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.