为什么我只为某些表单字段出错?

时间:2011-08-24 19:44:36

标签: php html forms

我最近上传了一个带有php脚本的html表单到godaddy服务器,在填写表单字段后,我收到一个错误,其中在一组行的末尾有一个“意外的';'”。经过一些故障排除,来自编程和搜索PHP.net网站的家庭成员的帮助,我将“||”更改为“&&”并且错误不再发生。但是,我收到了一个不同的错误,并没有进行任何其他更改,而不是我描述的。这是错误:

我们非常抱歉,但您提交的表单中发现了错误。这些错误显示在下面。

“您输入的电子邮件地址似乎无效。 您输入的名字似乎无效。 您输入的地址似乎无效。 您输入的邮政编码似乎无效。 您输入的电子邮件似乎无效。 您输入的电子邮件(在验证字段中)似乎无效。 您输入的电话号码似乎无效。 您输入的单元格号似乎无效。 您输入的传真号码似乎无效。 您输入的时间范围似乎无效。 数量似乎无效。 您输入的特定单位似乎无效。 您输入的请求完成日期似乎无效。 您输入的帐单地址似乎无效。 您输入的结算邮政编码似乎无效。

请返回并修正这些错误。

我检查了我的html中字符的最大长度,以及表格字段的其他设置似乎无效,并且据我所知,没有任何设置被破坏或配置错误。以下是这些行的HTML和PHP(您还需要更多信息吗?):

Link to HTML form:

PHP代码:

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "presto423@yahoo.com";
$email_subject = "Request Form Data submitted by...";


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['fname']) &&
    !isset($_POST['minit']) &&
    !isset($_POST['lname']) &&
    !isset($_POST['cname']) &&
    !isset($_POST['address']) &&
    !isset($_POST['city']) &&
    !isset($_POST['state']) &&
    !isset($_POST['zipcode']) &&
    !isset($_POST['email_from']) &&
    !isset($_POST['vemail']) &&
    !isset($_POST['phone']) &&
    !isset($_POST['cell']) &&
    !isset($_POST['fax']) &&
    !isset($_POST['workbefore']) &&
    !isset($_POST['referral']) &&
    !isset($_POST['services']) &&
    !isset($_POST['timeframe']) &&
    !isset($_POST['budget']) &&
    !isset($_POST['position']) &&
    !isset($_POST['quantity']) &&
    !isset($_POST['units']) &&
    !isset($_POST['specunits']) &&
    !isset($_POST['reqcompdate']) &&
    !isset($_POST['comments']) &&
    !isset($_POST['baddress']) &&
    !isset($_POST['bcity']) &&
    !isset($_POST['bstate']) &&
    !isset($_POST['bzipcode']))

$fname = $_POST["fname"];
$minit = $_POST["minit"];
$lname = $_POST["lname"];
$cname = $_POST["cname"];
$address = $_POST["address"];
$city = $_POST["city"];
$state = $_POST["state"];
$zipcode = $_POST["zipcode"];
$email_from = $_POST["email_from"];
$vemail = $_POST["vemail"];
$phone = $_POST["phone"];
$cell = $_POST["cell"];
$fax = $_POST["fax"];
$workbefore = $_POST["workbefore"];
$referral = $_POST["referral"];
$services = $_POST["services"];
$timeframe = $_POST["timeframe"];
$budget = $_POST["budget"];
$position = $_POST["position"];
$quantity = $_POST["quantity"];
$units = $_POST["units"];
$specunits = $_POST["specunits"];
$reqcompdate = $_POST["reqcompdate"];
$comments = $_POST["comments"];
$baddress = $_POST["baddress"];
$bcity = $_POST["bcity"];
$bstate = $_POST["bstate"];
$bzipcode = $_POST["bzipcode"];

$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,$fname)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$minit)) {
$error_message .= 'The Middle Initial you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$lname)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$cname)) {
$error_message .= 'The Company Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$address)) {
$error_message .= 'The Address you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$city)) {
$error_message .= 'The City you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$state)) {
$error_message .= 'The State you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$zipcode)) {
$error_message .= 'The Zip Code you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$email_from)) {
$error_message .= 'The Email you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$vemail)) {
$error_message .= 'The Email (in the verify field) you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$phone)) {
$error_message .= 'The Phone Number you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$cell)) {
$error_message .= 'The Cell Number you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$fax)) {
$error_message .= 'The Fax Number you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$workbefore)) {
$error_message .= 'Your entry does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$referral)) {
$error_message .= 'The Referred By you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$timeframe)) {
$error_message .= 'The Time Frame you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$position)) {
$error_message .= 'The Position you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$quantity)) {
$error_message .= 'The Quantity does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$units)) {
$error_message .= 'The Units you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$specunits)) {
$error_message .= 'The Specific Units you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$reqcompdate)) {
$error_message .= 'The Requested Completion Date 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(!preg_match($string_exp,$baddress)) {
$error_message .= 'The Billing Address you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$bcity)) {
$error_message .= 'The Billing City you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$bstate)) {
$error_message .= 'The Billing State you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$bzipcode)) {
$error_message .= 'The Billing Zip Code you entered does 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($fname)."\n";
$email_message .= "Middle Initial: ".clean_string($minit)."\n";
$email_message .= "Last Name: ".clean_string($lname)."\n";
$email_message .= "Company Name: ".clean_string($cname)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Zip Code: ".clean_string($zipcode)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Verify Email: ".clean_string($vemail)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Cell: ".clean_string($cell)."\n";
$email_message .= "Fax: ".clean_string($fax)."\n";
$email_message .= "Have you ever worked with Home Turf before? ".clean_string($workbefore)."\n";
$email_message .= "How did you hear about Home Turf? ".clean_string($referral)."\n";
$email_message .= "Services Being Requested: ".clean_string($services)."\n";
$email_message .= "Time Frame: ".clean_string($timeframe)."\n";
$email_message .= "Budget Range: ".clean_string($budget)."\n";
$email_message .= "Your Position: ".clean_string($position)."\n";
$email_message .= "Quantity: ".clean_string($quantity)."\n";
$email_message .= "Units: ".clean_string($units)."\n";
$email_message .= "other units, please specify: ".clean_string($specunits)."\n";
$email_message .= "Requested Completion Date: ".clean_string($reqcompdate)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
$email_message .= "Billing Address: ".clean_string($baddress)."\n";
$email_message .= "Billing City: ".clean_string($bcity)."\n";
$email_message .= "Billing State: ".clean_string($bstate)."\n";
$email_message .= "Billing Zip Code: ".clean_string($bzipcode)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

感谢您的时间和考虑。

2 个答案:

答案 0 :(得分:1)

我发现了两件事:

  1. 您不应该将||更改为&&if条件检查是否有任何字段为空,并且您检查是否所有是空的。

  2. 然后,整个下一个块应该包含在{}中,或者缺少一个代码块。看来如果任何字段为空(!isset()),则执行一些错误条件,如果所有字段都填写完毕,它应该进入最后一个长块并验证每个字段。

    < / LI>

    // Possibly something like:
    // validation expected data exists
    if(!isset($_POST['fname']) ||
        !isset($_POST['minit']) ||
        !isset($_POST['lname']) ||
        ...
        ...
        !isset($_POST['bstate']) ||
        !isset($_POST['bzipcode']))
    {
      // Echo an error that all fields must be filled in.
    }
    else 
    {
      // All were filled in, execute the rest and validate
      $fname = $_POST["fname"];
      $minit = $_POST["minit"];
      $lname = $_POST["lname"];
      $cname = $_POST["cname"];
      $address = $_POST["address"];
      $city = $_POST["city"];
      $state = $_POST["state"];
      // etc...
    }
    

答案 1 :(得分:1)

那一定是某种形式! : - )

说真的,它可能会帮助您减少此代码的大小。首先,为什么不将这些预期的变量名称存储在数组中:

$vars = array('fname', 'minit', ...);

然后你可以用来a)检查它们是否已被发送:

foreach ($vars as $var)
{
    if (!isset($var))
    {
        // Do whatever happens if at least one value hasn't been sent
    }
}

b)设置那些局部变量:

foreach ($vars as $var)
{
    $$var = $_POST[$var];
}

(虽然这确实没有必要;为什么不参考$_POST['fname']等?)

c)与另一个数组或更复杂的初始数组一起处理验证,例如

define('RE_STRING', "/^[A-Za-z .'-]+$/");

$vars = array
(
    'fname' => array('type' => RE_STRING, 'full' => 'First Name'),
    ...
);

foreach ($vars as $var => $details)
{
    if(!preg_match($details['type'], $_POST[$var])) {
        $error_message .= 'The '.$details['full'].' you entered does not appear to be valid.<br />';
    }
}

d)构建电子邮件:

foreach ($vars as $var => $details)
{
    $email_message .= $details['full'].": ".clean_string($_POST[$var])."\n";
}

如果将所有内容组合到单个foreach循环中,您将节省大量代码并减少导致很多麻烦的微小拼写错误的可能性。