1

在我的网站上,我有很多表格和字段,我有一个运行良好但非常痛苦的系统,并且启动和运行起来非常痛苦。

这就是我的系统所做的,一旦您发送表单,所有信息都会发送到拥有所有数据并对其进行验证的类。它还像这样存储字段的值$_SESSION['userUpdate']['firstName'][1] = $firstName;

如果出现错误,它会创建一个像这样$_SESSION['userUpdate']['firstName'][0] = 1;的会话变量 1 表示该字段为空。如果没有错误,会话变量将为 0。

如果在验证过程中没有发现错误,则将数据发送到数据库。

之后,表单页面重新加载:

header( 'HTTP/1.1 303 See Other' );
header( 'Location: '.curPageURL().'' ); 

我使用它是为了在重新加载页面时无法重新发送数据。这就是我使用所有这些会话变量的原因。

然后使用很多 it/else if/else 检查会话变量的值和输出错误,并使用之前输入的数据填充表单。

让我向您展示一个名字字段的示例。

这是 HTML 代码:

<label for="firstName" class="block">First Name</label>
<span>Your first name goes here.</span><?php echo $text_first_name ?>
<input type="text" id="firstName" class="mediaText" name="firstName" value="<?php echo $first_name2; ?>" onchange="needToConfirm=true" <?php echo $style_first_name ?> />

这是类的验证过程:

$_SESSION['ui']['first_name'][1] = $this->first_name; 
if (isset($this->first_name)) 
    {
        if($this->first_name == NULL)
        {
            $_SESSION['ui']['first_name'][0] = 1;
        }
        else if(minRange(3, $this->first_name)) 
        {   
            $_SESSION['ui']['first_name'][0] = 2; 
        }
        else
        {
            array_push($set, "FirstName = '".$db->sql_escape($this->first_name)."'");
        }
    }

这是处理最终错误的 php 代码:

$error_bg = "style=\"background:#EE5C42\"";

//FIRST NAME
if($_SESSION['ui']['first_name'][0] == 1)
{
    $style_first_name = $error_bg;
    $first_name2 = $_SESSION['ui']['first_name'][1];
}
else if($_SESSION['ui']['first_name'][0] == 2)
{
    $style_first_name = $error_bg;
    $first_name2 = $_SESSION['ui']['first_name'][1];
    $text_first_name = "<span class=\"errorText\">Your first name must consist of at least 3 characters.</span>";
}
else
{
    $first_name2 = $userdetails["FirstName"];
}

在页面的末尾有一个小功能可以取消设置会话变量。

我想知道有什么方法可以让这个更简单、更容易启动和运行?

4

2 回答 2

1

如果您要寻求代码优化建议,我会这样做:

// so you have bunch of error codes
$possible_errors = array(
  '0' => '', // No error
  '1' => '', // Error, but no error message
  '2' => 'Your %s must consists of at least 3 characters',
  ...
);

// then you have form fields stored in session
// fields in session should be named like the keys in $userdetails
// 'formName' => array('FirstName' => array(0, 'German'), 'LastName' => array('1', ''))
$errors = $values = array();
foreach ($_SESSION['formName'] as $field => $element) {
    if ($element[0] > 0) { // Has error
        $error_code = $element[0];
        $error_message = $possible_errors[$error_code];
        if (!empty($error_message)) {
            $errors[$field] = sprintf($error_message, $field);
        }
    } else {
        $values[$field] = $userdetails[$field];
    }
}

// in here you end up with two arrays:
//   - $errors Error messages keyed by field name
//   - $values Values keyed by field name
// You use them like this
<label for="firstName" class="block">First Name</label>
<span>Your first name goes here.</span><?php if (array_key_exists('FirstName', $errors)) echo $errors['FirstName']; ?>
<input type="text" id="firstName" class="mediaText" name="firstName" 
  value="<?php echo $values['FirstName']; ?>"
  onchange="needToConfirm=true"
  <?php if(array_key_exists('FirstName', $errors)):?>style="background:#EE5C42"<?php endif;?> />
于 2012-07-02T21:35:30.027 回答
0

我建议使用非常好的Bassistance jQuery Validate 插件添加一些客户端验证。

于 2012-07-02T23:30:16.407 回答