联系表不工作,无法直接访问
Contact Form Not Working and coming up with no direct access
我觉得好像一切都正确完成了,但是在提交表单时却无法直接访问。这是来自一个没有联系表格的模板网站,所以我在网上找到了一个,但似乎无法使用它。
这里是 html
<form action="mail.php" method="post" class="wow fadeInUp" data-wow-delay="0.6s">
<div class="col-md-6 col-sm-6">
<input type="text" name="name" class="form-control" placeholder="Your Name...">
</div>
<div class="col-md-6 col-sm-6">
<input type="email" name="email" class="form-control" placeholder="Your Email...">
</div>
<div class="col-md-12 col-sm-12">
<textarea rows="6" name="message" class="form-control" placeholder="Your Message" required="">
</textarea>
</div>
<div class="col-md-offset-4 col-md-8 col-sm-offset-4 col-sm-8">
<input type="submit" class="form-control" value="SEND">
</div>
</form>
这是php
<?php
/**
* sends mail submitted from the contact form
*/
/*
EDIT BELOW
*/
$to_Email = "appydevelopers@gmail.com"; //Replace with your email address
$subject = 'Appydeveloper Site'; //Subject line for emails
/*
EDIT ABOVE
*/
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')
die("No direct access.");
if(!isset($_POST["name"]) || !isset($_POST["email"]) || !isset($_POST["message"])) {
$output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
die($output);
}
//additional validation
$user_Name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$user_Message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
if(strlen($user_Name)<4) {
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) {
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(strlen($user_Message)<5) {
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
$sentMail = @mail($to_Email, $subject, $user_Message .' -'.$user_Name, $headers);
if(!$sentMail) {
$output = json_encode(array('type'=>'error', 'text' => 'Server error, could not send email. Sorry for the inconvenience.'));
die($output);
} else {
$output = json_encode(array('type'=>'success', 'text' => 'Message successfully sent!'));
die($output);
}
?>
当您不通过 Ajax 调用此 php 文件时,为什么要在此处检查它 isset($_SERVER['HTTP_X_REQUESTED_WITH'])
当您通过 Ajax 调用时使用此文件。所以根本就没有设置。
评论这一行并检查
//if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') //die("No direct access.");
我觉得好像一切都正确完成了,但是在提交表单时却无法直接访问。这是来自一个没有联系表格的模板网站,所以我在网上找到了一个,但似乎无法使用它。
这里是 html
<form action="mail.php" method="post" class="wow fadeInUp" data-wow-delay="0.6s">
<div class="col-md-6 col-sm-6">
<input type="text" name="name" class="form-control" placeholder="Your Name...">
</div>
<div class="col-md-6 col-sm-6">
<input type="email" name="email" class="form-control" placeholder="Your Email...">
</div>
<div class="col-md-12 col-sm-12">
<textarea rows="6" name="message" class="form-control" placeholder="Your Message" required="">
</textarea>
</div>
<div class="col-md-offset-4 col-md-8 col-sm-offset-4 col-sm-8">
<input type="submit" class="form-control" value="SEND">
</div>
</form>
这是php
<?php
/**
* sends mail submitted from the contact form
*/
/*
EDIT BELOW
*/
$to_Email = "appydevelopers@gmail.com"; //Replace with your email address
$subject = 'Appydeveloper Site'; //Subject line for emails
/*
EDIT ABOVE
*/
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')
die("No direct access.");
if(!isset($_POST["name"]) || !isset($_POST["email"]) || !isset($_POST["message"])) {
$output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
die($output);
}
//additional validation
$user_Name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$user_Message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
if(strlen($user_Name)<4) {
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) {
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(strlen($user_Message)<5) {
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
$sentMail = @mail($to_Email, $subject, $user_Message .' -'.$user_Name, $headers);
if(!$sentMail) {
$output = json_encode(array('type'=>'error', 'text' => 'Server error, could not send email. Sorry for the inconvenience.'));
die($output);
} else {
$output = json_encode(array('type'=>'success', 'text' => 'Message successfully sent!'));
die($output);
}
?>
当您不通过 Ajax 调用此 php 文件时,为什么要在此处检查它 isset($_SERVER['HTTP_X_REQUESTED_WITH'])
当您通过 Ajax 调用时使用此文件。所以根本就没有设置。
评论这一行并检查
//if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') //die("No direct access.");