0

我有十几个使用heredoc语句来显示HTML的功能齐全的脚本,包括例如$errors[field_name]但是这个几乎相同的脚本会导致POST错误,例如

“注意:未定义的索引:第 162 行 register.php 中的 user_password”

当用户在需要 8 个以上字符的情况下输入 5 个字符的密码时,会填充验证功能$errors['password'],并且表单应该与验证错误一起重新显示。

在这种 Bootstrap 样式表单中看不到我缺少什么。该$errors数组是全局的,我可以强制显示错误,所以我知道错误已初始化,但我不断收到

“注意:未定义的索引”

$fields = array( 'user_name', 'user_email', 'user_password', 'user_password2' );

function display_form($string = '')
{
    global $error_token, $cfg, $fields, $form, $errors, $s, $token, $validation_status;
    $error_message = '';
    switch($_SERVER['REQUEST_METHOD'])
    {
        Case 'POST':
            // validation failed, redisplay the form
            $autofocus = '';
            foreach($fields as $field){
                $$field = htmlentities(get_cgi_var($field), ENT_QUOTES, 'UTF-8');
                // wrap errors in Bootstrap alert markup
                $errors[$field] = !empty($errors[$field]) ? '<div class="alert alert-danger">'.htmlentities($errors[$field], ENT_QUOTES, 'UTF-8').'</div>' : '';
                // debug, force the premature display of errors.
                // echo !empty($errors[$field]) ? '<div class="alert alert-danger">'.htmlentities($errors[$field], ENT_QUOTES, 'UTF-8').'</div>' : '<p>empty</p>';
            }
            break;
        Case 'GET':
        default:
            $autofocus = 'autofocus';
            foreach($fields as $field){
                $$field = '';
                $errors[$field] = '';
            }
            $error_token = '';
            break;
    };
    foreach($fields as $field){
        $validation_status[$field] = empty($errors[$field]) ? ' has-success' : ' has-error';
    }
    $self = SELF;

print<<<_HTML_
<div class="col-lg-6">
  <div class="panel panel-primary">
    <div class="panel-heading">Registration</div>
    <div class="panel-body">
      $string
      $error_message
      <form name="form1" id="form1" action="$self" class="form-horizontal" role="form" method="POST">
      <fieldset>
      $error_token
      $errors[user_name]
      <div class="row form-group$validation_status[user_name]">
        <label for="user_name" class="col-lg-3 control-label">Name</label>
        <div class="col-lg-9">
          <input type="text" name="user_name" id="user_name" value="$user_name" maxlength="255" $autofocus required placeholder="Your First &amp; Last Name" title="Your First &amp; Last Name" autocomplete="on" class="form-control">
        </div>
      </div>
      $errors[user_email]
      <div class="row form-group$validation_status[user_email]">
        <label for="user_email" class="col-lg-3 control-label">Email</label>
        <div class="col-lg-9">
          <input type="email" name="user_email" id="user_email" value="$user_email" maxlength="255" required placeholder="Your Email Address" autocomplete="on" class="form-control">
        </div>
      </div>
      $errors[user_password]
      <div class="row form-group$validation_status[user_password]">
        <label for="user_password" class="col-lg-3 control-label">Password</label>
        <div class="col-lg-9">
          <input type="text" name="user_password" id="user_password" value="$user_password" maxlength="255" required placeholder="Password or Passphrase" autocomplete="on" class="form-control">
          <p class="help-block">Case sensitive password containing 8+ characters or a phrase containing 3-5 words</p>
        </div>
      </div>
      $errors[user_password2]
      <div class="row form-group$validation_status[user_password2]">
        <label for="user_password2" class="col-lg-3 control-label">Password</label>
        <div class="col-lg-9">
          <input type="text" name="user_password2" id="user_password2" value="$user_password2" maxlength="255" required placeholder="Re-enter Password or Passphrase" autocomplete="on" class="form-control">
        </div>
      </div>
      <div class="row form-group">
        <label for="submit_button" class="col-lg-3 control-label">&nbsp;</label>
        <div class="col-lg-9 text-center">
          <input type="hidden" name="token" value="$token">
          <input type="submit" name="submit_button" id="submit_button" value="Submit" onClick="return captcha_validation(this.form);" class="btn btn-primary">
        </div>
      </div>
      </fieldset>
      </form>
      </div><!-- /panel-body -->
      <div class="panel-footer text-center">&nbsp;</div>
  </div><!-- /panel -->
</div><!-- /col -->
<div class="col-lg-6">
  <div class="well">unused column</div>
</div><!-- /col -->
_HTML_;
};
4

1 回答 1

0

这是你的问题:

default:
    $autofocus = 'autofocus';
    foreach($fields as $field){
        $$field = ''; // <------------------
        $errors[$field] = '';
    }
    $error_token = '';
    break;

完全替换或删除它 - 这里不需要它$$field$field

于 2013-11-01T17:56:59.823 回答