6

我正在处理一个用 php 编写的简单联系表单,并且几乎所有设置都按照我的意愿进行设置,除了现在,所有验证错误都显示在表单顶部的 div 中,我想更改下面的代码,因此它们显示在导致错误的表单字段旁边,但我不确定如何完成此操作,因此我向专家寻求建议。

PHP 验证例程

$frm_valid = new Validate();

if(isset($_POST['submit'])){

$rules=array(
    array('Type'=>'Required','Var'=>$_POST['name'],'Msg'=>'Name field is required.'),
    array('Type'=>'Required','Var'=>$_POST['email'],'Msg'=>'Email address field is required.'),
    array('Type'=>'Email','Var'=>$_POST['email'],'Msg'=>'Your email address format is invalid.'),
    array('Type'=>'Required','Var'=>$_POST['subject'],'Msg'=>'Subject field is required.'),
    array('Type'=>'Required','Var'=>$_POST['message'],'Msg'=>'Message field is required.'),
);


$result = $frm_valid->Valid($rules);

if(is_array($result)){
    // Print validation errors (if any)
    echo('<div"><b>The form cannot be submitted until the following errors are corrected.</b><ul>');

    foreach($result as $key=>$val){
      echo '<li>'.$val.'</li>';
    }
    echo('</ul></div><br />');
  } 
  else {
    // Do something else.
  }
}

表单 HTML

<form action="<? echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <input type="text" name="name" value="" /> <br />

    <input type="text" name="email" value="" />  <br />

    <input type="text" name="subject" value="" /> <br />

    <textarea name="message" cols="80" rows="7"></textarea> <br />

    <input type="submit" name="submit" value="Send" />
</form>
4

2 回答 2

7

您需要为此更改整个代码!!!结构,也许是这样的:

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
    <ul>
        <li>
            <label>
                <span>Name</span>
                <input type="text" name="name" />
                <small class="errorText"><?php echo ($_POST["name"] == "") ? "This field is required" : ""; ?></small>
            </label>
        </li>
        <li>
            <label>
                <span>Email</span>
                <input type="text" name="email" />
                <small class="errorText"><?php echo ($_POST["email"] == "") ? "This field is required" : ""; ?></small>
            </label>
        </li>
        <li>
            <label>
                <span>Subject</span>
                <input type="text" name="subject" />
                <small class="errorText"><?php echo ($_POST["subject"] == "") ? "This field is required" : ""; ?></small>
            </label>
        </li>
        <li>
            <label>
                <span>Message</span>
                <textarea name="message" cols="80" rows="7"></textarea>
                <small class="errorText"><?php echo ($_POST["message"] == "") ? "This field is required" : ""; ?></small>
            </label>
        </li>
        <li>
            <input type="submit" name="submit" value="Send" />
        </li>
    </ul>
</form>

和相同的CSS:

* {font-family: Segoe UI, Tahoma;}
h1 {font-weight: bold; font-size: 14pt; padding: 5px 0; margin: 5px 0; border: 1px solid #999; border-width: 1px 0;}
input[type='submit'] {padding: 5px 20px; cursor: pointer;}
ul, li {display: block; list-style: none; margin: 0; padding: 0;}
ul li {padding: 5px 0;}
ul li label span {display: block; cursor: pointer;}
ul li label .errorText {color: #f00; font-weight: bold; vertical-align: top;}
ul li label textarea {width: 300px;}

你可以在这里看到一个现场演示:Demo

PHP 中的错误处理

为每个字段保留一个错误变量。说,

<?php
    $error = array(
        "name" => "",
        "email" => "",
        "subject" => "",
        "message" => ""
    );
?>

用错误更新它们并在下面显示它们。

<?php
    if (empty()$_POST["email"])
        $error["email"] = "Email is required!";
    elseif (!isEmail($_POST["email"]))
        $error["email"] = "Not a Valid Email!";
?>

如果没有错误,它将为空,并且用户不会看到错误消息。在您的表单代码中,您只需以这种方式更新:

<small class="errorText"><?php echo $error["name"]; ?></small>
<small class="errorText"><?php echo $error["email"]; ?></small>

在后端的位置,$error["email"]将有"This field is required"or "Not a valid email address!"

于 2012-06-28T03:30:47.107 回答
0

如果您的每个字段都有一个错误,您可以使用以下方式:

  <input name='myfield'>
     @if ($errors->has('myfield'))
        <span style="color:red;">
            {{ $errors->first('myfield') }}
        </span>
     @endif
于 2017-02-19T14:37:40.520 回答