PHP表单验证不会在提交时执行

时间:2015-01-19 19:14:24

标签: php

好的,我有一个php表单。标准,填写,提交,检查错误,传递和发送电子邮件,重定向到感谢页面。

除了验证之外,我的表单运行良好。如果我填写表单并点击发送,我会收到电子邮件,页面会重定向。如果我没有填写表格并点击发送,我没有收到错误,我没有收到电子邮件,但页面确实重定向。

为什么我的验证不起作用?我错过了电子邮件部分和PHP验证部分之间的链接吗?

<?php
// define variables and set to empty values
 $company = $fname = $lname = $email = $phone = $address = $city = $provincestate = $country = $location = $size = $type = $message ="";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $company = test_input($_POST["company"]);
  $fname = test_input($_POST["first-name"]);
  $lname = test_input($_POST["last-name"]);
  $email = test_input($_POST["email"]);
  $phone = test_input($_POST["phone"]);
  $address = test_input($_POST["address"]);
  $city = test_input($_POST["city"]);
  $provincestate = test_input($_POST["provincestate"]);
  $country = test_input($_POST["country"]);
  $location = test_input($_POST["location"]);
  $size = test_input($_POST["size"]);
  if(isset($_POST["type"])){ $type = $_POST["type"];}
  $message = test_input ($_POST["message"]);
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

// define variables and set to empty values
 $companyErr = $fnameErr = $lnameErr = $emailErr = $phoneErr = $addressErr = $cityErr = $provincestateErr = $countryErr = $locationErr = $sizeErr = $typeErr = $messageErr ="";
 $company = $fname = $lname = $email = $phone = $address = $city = $provincestate = $country = $location = $size = $type = $message ="";


if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if (empty($_POST["company"])) {
    $company = "";
  } else {
    $company = test_input($_POST["company"]);
  } 

   if (empty($_POST["first-name"])) {
    $fnameErr = "First name is required";
  } else {
    $fname = test_input($_POST["first-name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
      $fnameErr = "Only letters and white space allowed"; 
    }
  }     

  if (empty($_POST["last-name"])) {
    $lnameErr = "Last name is required";
  } else {
    $lname = test_input($_POST["last-name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
      $lnameErr = "Only letters allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
  }

  if (empty($_POST["phone"])) {
    $phoneErr = "Phone number is required";
  } else {
    $phone = test_input($_POST["phone"]);
    // check if phone number only contains 10 digits with no formatting
    if (!preg_match("/^[0-9]{10}+$/",$phone)) {
      $phoneErr = "Only enter a 10 digit number"; 
    }
  }

  if (empty($_POST["address"])) {
    $address = "";
  } else {
    $address = test_input($_POST["address"]);
  } 

   if (empty($_POST["city"])) {
    $city = "";
  } else {
    $city = test_input($_POST["city"]);
  }

  if (empty($_POST["provincestate"])) {
    $provincestate = "";
  } else {
    $provincestate = test_input($_POST["provincestate"]);
  }

  if (empty($_POST["country"])) {
    $country = "";
  } else {
    $country = test_input($_POST["country"]);
  }


  if (empty($_POST["location"])) {
    $locationErr = "Location is required";
  } else {
    $location = test_input($_POST["location"]);
    // check if location only contains letters
    if (!preg_match("/^[a-zA-Z ]*$/",$location)) {
      $locationErr = "Please enter a city"; 
    }
  }

 if (empty($_POST["size"])) {
    $sizeErr = "Please enter a number";
  } else {
    $size = test_input($_POST["size"]);
  }

  if (empty($_POST["type"])) {
    $typeErr = "Please select 1";
  } else {
    $type = test_input($_POST["type"]);
  }

   if (empty($_POST["message"])) {
    $message = "";
  } else {
    $message = test_input($_POST["message"]);
  }
}

$myemail = '';//<-----Put Your email address here.

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $to = $myemail; 
    $email_subject = "Inquiry from: $fname $lname";
    $email_body = "You have received a new inquiry from:".
    "\n
     \n Name: $fname $lname \n Email: $email \n Phone Number: $phone
     \n Address: $address \n City: $city \n Province/State: $provincestate \n Country: $country
     \n I have a project in: $location \n The project type is: $type  \n The estimated project size is: $size
     \n Message: $message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email";

    mail($to,$email_subject,$email_body,$headers);

    //redirect to the 'thank you' page
    header('Location: thankyou.html');
    exit();
}
?>

3 个答案:

答案 0 :(得分:1)

验证不起作用,因为没有任何内容可以输出验证消息,例如:

if($emailErr != '') die($emailErr);

我建议你创建一个这样的函数:

$errorMessages = array();
function errormsg($message) {
  $errorMessages[] = $message;
}

然后,不要在$fnameErr = "First name is required";之类的变量中编写vaidation消息,而是调用此函数:

errormsg('First name is required');

最后,如果出现错误而不是发送电子邮件,您可以输出消息:

if(count($errorMessages) > 0) {
  /* output the messages */
  print_r($errorMessages); /* for illustration */
} else {
  /* send the email */
}

修改 阅读评论我得出的结论是,有许多关于验证的代码尚未向我们展示。因此,如果您想使用当前逻辑,则需要围绕电子邮件发送部分发出if声明,检查是否存在验证错误。

if($fnameErr != "" && $lnameErr != "" /* and so on */) {
  /* send email */
}

答案 1 :(得分:1)

在验证结束时你没有任何回报,所以它只是通过它们,并访问最后一个if语句(发送邮件的地方),到达header('Location: thankyou.html')行。

编辑:

您应该重定向到主窗体(例如,当任何验证不起作用时,使用header('Location: yourform.html')

if (empty($_POST["last-name"])) {
    $lnameErr = "Last name is required";
    header('Location: yourform.html');
} else {
    $lname = test_input($_POST["last-name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
        $lnameErr = "Only letters allowed";
        header('Location: yourform.html'); 
    }
}

其他选项是收集所有错误(例如,有错误计数),然后重定向,如果发现任何错误,显示它们。

if (empty($_POST["last-name"])) {
    $lnameErr = "Last name is required";
    $errCount++;
} else {
    $lname = test_input($_POST["last-name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
        $lnameErr = "Only letters allowed";
        $errCount++;
    }
}
...
if($errCount > 0){
    ...
    header('Location: yourform.html');
}

另一种选择是使用JavaScript直接在表单中处理验证。

答案 2 :(得分:0)

$myemail = '';//<-----Put Your email address here.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    //add these error showing
    if($companyErr) exit("Error : $companyErr");
    if($fnameErr) exit("Error : $fnameErr");
    //repeat all those variables here......

    $to = $myemail; 
    $email_subject = "Inquiry from: $fname $lname";
    $email_body = "You have received a new inquiry from:".
    "\n
     \n Name: $fname $lname \n Email: $email \n Phone Number: $phone
     \n Address: $address \n City: $city \n Province/State: $provincestate \n Country: $country
     \n I have a project in: $location \n The project type is: $type  \n The estimated project size is: $size
     \n Message: $message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email";

    mail($to,$email_subject,$email_body,$headers);

    //redirect to the 'thank you' page
    header('Location: thankyou.html');
    exit();
}