0

我在我的网站上添加了一个简单的联系表单,但是当我单击提交时,我被带到了 action 属性的 url。以下是我的表单的 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');
} 

}
?>

谁能明白为什么会发生这种情况?

4

1 回答 1

1

您的问题在于这一行:

if(isset($_POST['email'])) {

您的表单没有输入名称,email因此此测试永远不会为真,整个脚本都在此条件内。您正在提交$_POST['first_name'],或者$_POST['telephone']您应该检查这两个是否之一。

于 2013-09-11T13:20:21.743 回答