任何想法为什么此表单验证触发错误?

时间:2013-10-03 21:00:51

标签: php forms validation

我正在尝试验证表单中的3个文本框,出于某种原因,如果只有一个验证错误,我会一直收到PHP错误。如果我触发多个验证错误,它可以正常工作。

if (isset($_POST['origin_zip'], $_POST['dest_zip'], $_POST['weight'])) {
 $error = array();
 $origin_zip = $_POST['origin_zip'];
 $dest_zip = $_POST['dest_zip'];
 $weight = $_POST['weight'];

if (empty($origin_zip) || empty($dest_zip) || empty($weight)) {
 $errors[] = 'Origin Zip, Destination Zip and Weight for at least one item is required.';
     $errors = implode($errors);
     echo $errors;
 } else {
    if ((!isset($origin_zip)) || (strlen($origin_zip) != 5) || (!is_numeric($origin_zip))){
      $errors[] = 'Origin Zip must be 5 digits long and contain only numbers.';
    }
    if ((!isset($dest_zip)) || (strlen($dest_zip) != 5) || (!is_numeric($dest_zip))){
      $errors[] = 'Destination Zip must be 5 digits long and contain only numbers.';
    }
    if (empty($weight)) {
      $errors[] = 'Weight cannot be left empty.';
    }
    if ($weight > 15000) {
      $errors[] = 'Weight cannot exceed 15000 lbs.';
    }
    if (!is_numeric($weight)) {
      $errors[] = 'Weight must be a number.';
    }
   }
   if (!empty($errors)) {
    foreach ($errors as $value) {
     echo $value.'<br />';
    }
   } else {
   echo 'Redirect to request.php';
   }
}

2 个答案:

答案 0 :(得分:1)

您没有定义$ errors值。您将其定义为$ error。

$error = array();

成功:

$errors = array();

答案 1 :(得分:0)

您的错误来自$error = array();这应该是$errors