>我已经在我的网站上添加了一个简单的联系表单,但是当我点击提交时,我会被带到操作属性的网址。以下是我的表单的 html:
<form name="quickcontact" method="post" action="includes/quick_contact.php" id="quickcontact">
<input type="text" name="first_name" maxlength="50" size="30" placeholder="Name">
<input type="text" name="telephone" maxlength="30" size="30" placeholder="Number">
<input type="submit" value="Call Me" id="submit">
</form>
以及引用的 php 文件:
<?php
if(isset($_POST['email'])) {
$email_to = "sam.skirrow@gmail.com";
$email_subject = "AAO - Someone wants you to call them back";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['telephone'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$telephone = $_POST['telephone']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+'.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.'n'n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."'n";
$email_message .= "Telephone: ".clean_string($telephone)."'n";
$headers = 'From: '.$email_from."'r'n".
'Reply-To: '.$email_from."'r'n" .
'X-Mailer: PHP/' . phpversion();
if (mail($email_to, $email_subject, $email_message, $headers)) {
header('location: ../success.php');
} else {
header('location: ../fail.php');
}
}
?>
谁能明白为什么会发生这种情况??
您的问题与以下行有关:
if(isset($_POST['email'])) {
您的表单没有带有名称的输入 email
因此此测试永远不会为真,并且整个脚本都在此条件下。您正在提交$_POST['first_name']
或$_POST['telephone']
您应该检查这两个是否是之一。