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;}

ここでライブデモを見ることができます:デモ

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"かがあります"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 に答える