php联系表单提交只需要我转到操作URL

时间:2013-09-11 12:54:17

标签: php email action

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

}
?>

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

1 个答案:

答案 0 :(得分:1)

你的问题在于这一行:

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

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