0

这是功能在我的功能页面上占用了很多行。

foreach除非将它们放入数组中,然后使用循环,否则我无法弄清楚如何将其取下来。但我希望它具体说明不允许为空白的内容。

if ($EmployeeID === "")
{
  echo "EmployeeID Is Blank.";

}
else
{
  if ($Firstname === "")
  {
    echo "Firstname Is Blank.";
  }
  else
  {
    if ($Lastname === "")
    {
      echo "Last Name Is Blank";

    }
    else
    {
      if ($PhoneNumber === "")
      {
        echo "Phone Number Is Blank";

      }
      else
      {
        if ($Address === "")
        {
          echo "Address Is Blank";

        }
        else
        {
          if ($City === "")
          {
            echo "City Is Blank";

          }
          else
          {
            if ($State === "")
            {
              echo "Sate Is Blank";

            }
            else
            {
              if ($Zip === "")
              {
                echo "Zip Is Blank";

              }
              else
              {
                if ($Email === "")
                {
                  echo "Email Is Blank";

                }
                else
                {
                  if ($Password === "")
                  {
                    echo "Password Is Blank";

                  }
                  else
                  {
                    echo "All Success";
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

我找到的解决方案将行记下来,但没有给我确切的关于哪个字段留空的信息是:

$Array = array();
$Array[] = $EmpID;
$Array[] = $FirstName;
$Array[] = $Lastname;
$Array[] = $PhoneNumber;
$Array[] = $Address;
$Array[] = $City;
$Array[] = $State;
$Array[] = $Zip;
$Array[] = $Email;
$Array[] = $Password;   
foreach ($Array AS $Checking) 
{
  if (empty($Checking))
  {
    echo "One Or More Is Left Blank"; 
    exit;
  }
}
4

7 回答 7

5
try{
    if(empty($EmployeeID)){
        throw new Exception('EmployeeID Is Blank.');
    }elseif(empty($Firstname)){
        throw new Exception('Firstname Is Blank.');
    }elseif(empty($Lastname)){
        throw new Exception('Lastname Is Blank.');
    }elseif(empty($PhoneNumber)){
        throw new Exception('PhoneNumber Is Blank.');
    }elseif(empty($Address)){
        throw new Exception('Address Is Blank.');
    }elseif(empty($City)){
        throw new Exception('City Is Blank.');
    }elseif(empty($State)){
        throw new Exception('State Is Blank.');
    }elseif(empty($Zip)){
        throw new Exception('Zip Is Blank.');
    }elseif(empty($Email)){
        throw new Exception('Email Is Blank.');
    }elseif(empty($Password)){
        throw new Exception('Password Is Blank.');
    }
    echo 'All Success';
}catch(Exception $e){
    echo $e->getMessage();
}
于 2012-11-19T18:03:32.260 回答
4

也许你可以像这样重新组织它:

$messages = array();

if (empty($EmployeeID)){
    $messages[] = "EmployeeID Is Blank."; 
}
if (empty($Firstname)){
    $messages[] = "Firstname Is Blank."; 
}

// and so on for the rest.

最后,您可以检查是否$messages为空来验证:

if (empty($messages)){
    echo "All Success";
}else {
   echo "Errors:";
   foreach($messages as $message){
       echo "$message <br>";
   }
}
于 2012-11-19T18:06:18.227 回答
2

你为什么要使用这些else陈述?我会一次显示所有错误以避免打扰用户(用户修复了错误,然后突然弹出另一个错误......)。

一个简单的例子:

echo empty($EmployeeID) ? 'EmployeeID Is Blank.' : '';
echo empty($Firstname) ? 'Firstname Is Blank.' : '';
//etc.

我可能会将所有错误消息添加到数组并循环遍历该数组/检查它是否为空:

$errors = array();

if (empty($EmployeeID))
{
  $errors['employeeid'] = 'EmployeeID Is Blank.';
}
if (empty($Firstname))
{
  $errors['firstname'] = 'Firstname Is Blank.';
}
// etc.

if (count($errors) > 0)
{
  // error handling
}

// and you can use the array keys to display the appropriate messages at the appropriate place if it is set:
if (isset($errors['employeeid']))
{
  // display the EmployeeID error message where the field should be filled in
}
于 2012-11-19T18:05:21.520 回答
2

您可以使用这样的关联数组

$formValues = array(
  'Employee ID' => $EmployeeID,
  'First Name' => $Firstname,
  'Last Name'=> $Lastname,
  'Phone Number'=> $PhoneNumber,
  'Address'=> $Address,
  'City'=> $City,
  'State'=> $State,
  'Zip'=> $Zip,
  'Email'=> $Email,
  'Password'=> $Password,
);
$error=false;

foreach ($formValues as $key => $value) {
  if(empty($value)) {
    echo($key.' is blank');
    $error=true;
  }
}
if(!$error) {
  echo("All Success");
}
于 2012-11-19T18:16:30.347 回答
1

将其全部包含在表单验证器中:

<?php
  function is_form_valid() {
    if ($Firstname === "") { return false; }
    if ($Lastname === "") { return false; }
    // ...

    return true;
  }
?>

当然,这只是强调了这个想法,您需要确定传递表单字段的最佳方法。然后将其用作主处理例程中的单个调用:

<?php

  if (is_form_valid()) {
    // do stuff
  } else {
    // report error
  }
?>

为了使它更有用,您可以为名称字段、电子邮件等中的有效输入创建一个共享类,并从您的表单验证中调用它。想象这样一个类存在,您的方法将如下所示:

<?php
  function is_form_valid() {
    if (! $validator->is_valid_name($Firstname)) { return false; }
    if (! $validator->is_valid_name($Lastname)) { return false; }
    if (! $validator->is_valid_email($Email)) { return false; }
    if (! $validator->is_valid_phone($Phone)) { return false; }
    // ...

    return true;
  }
?>

请注意姓名、电话号码、地址等方面的许多地区差异。您可以在此处阅读常见陷阱。这个论坛上还有几个关于数据验证主题的问题。

于 2012-11-19T18:03:05.860 回答
1
$errors = "";

if($EmployeeID == "")
{
    $errors .= "Employee ID blank<br />";
}

if($Firstname == "")
{
    $errors .= "First Name blank<br />";
}

if($errors != "")
{
    echo $errors;
}
于 2012-11-19T18:03:33.373 回答
1

大多数其他答案都更好,但你也应该学会使用elseif()

if ($EmployeeID === "") {
    echo "EmployeeID Is Blank.";
} elseif ($Firstname === "") {
    echo "Firstname Is Blank.";
} elseif ($Lastname === "") {
    echo "Last Name Is Blank";
} //etc
于 2012-11-19T18:30:48.980 回答