特定の条件が満たされない場合にエラー メッセージを作成しようとしています。したがって、ユーザーがフォームに入力し、フィールドが空であるか、検証に合格しない場合、エラー メッセージが返されます。
これは次の形式です。
if (isset($_POST)) {
if (checkEmail($email) == TRUE && $name != NULL && $surName != NULL) {
mysql_query( "INSERT INTO USR_INFO (NAME, MAIL, SURNAME)
VALUES ('$name', '$email','$surName') ") or die(mysql_error());
header('Location: thanks.php');
}
else {
echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">
<label for="name">First Name</label>
<input type="text" name="name" id="name" value="' .$_POST['name'].'" />
<span class="required">*</span>
<label for="surName">Last Name</label>
<input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" />
<span class="required">*</span>
<label for="email">E-mail</label>
<input type="email" id="email" name="email" placeholder="example@domain.com" value="' .$_POST['email']. '" />
<span class="required">*</span>
<input type="submit" name="submit" id="submit">
</form>';
}
} else {
echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">
<label for="name">First Name</label>
<input type="text" name="name" id="name" value="" />
<span class="required">*</span>
<label for="surName">Last Name</label>
<input type="text" name="surName" id="surName" value="" />
<span class="required">*</span>
<label for="email">E-mail</label>
<input type="email" id="email" name="email" placeholder="example@domain.com" value="" />
<span class="required">*</span>
<input type="submit" name="submit" id="submit">
</form>';
}
だから私が試したのは、次のようなエラーメッセージを表示する配列を追加することです:
$errorMessage = array();
そして、これを適切なメッセージとともに html フォーム フィールドに追加します。
$error[] = "Error Message";
今私が立ち往生しているのは、ユーザーが条件を満たさない場合にのみエラーを表示したいということです
if ($name == NULL) {$error[] = "Error Message";}
if ($surName == NULL) {$error[] = "Error Message 2";}
if (checkEmail($email) == FALSE || NULL) {$error[] = "Error Message 3";}
しかし、私はそれを機能させることはできません。このロジックを実装しようとすると、ページが正常に解析され、検証も機能しますが、必須フィールドを空白のままにするとエラー メッセージが表示されません。私の推測では、適切にループしていなかったと思います。
助けていただければ幸いです。
編集:
Frosty Z によって投稿された回答を試してみましたが、これが現時点で私が持っているものです:
if (isset($_POST)) {
$errorMessage = array();
if ($name == '') { $errors[] = "Input name please." }
if ($surName == '') { $errors[] = "Input last name please." }
if (!checkEmail($email)) { $errors[] = "Email address not valid." }
if (count($error) == 0) {
mysql_query( "INSERT INTO USR_INFO (NAME, MAIL, SURNAME)
VALUES ('$name', '$email', '$surName') ") or die(mysql_error());
header('Location: thanks.php');
exit;
else {
if (count($errors) > 0)
echo "<p>Sorry, there are problems with the information you have provided:</p>";
foreach($errors as $error)
echo '<p class="error">'.$error.'</p>';
echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">
<label for="name">Name</label>
<input type="text" name="name" id="name" value="' .$_POST['name'].'" />
<span class="required">*</span>
<label for="surName">Last name</label>
<input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" />
<span class="required">*</span>
<label for="email">E-mail</label>
<input type="email" id="email" name="email" placeholder="example@domain.com" value="' .$_POST['email']. '" />
<span class="required">*</span>
<input type="submit" name="submit" id="submit">
</form>';
}
} else {
echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">
<label for="name">Name</label>
<input type="text" name="name" id="name" value="" />
<span class="required">*</span>
<label for="surName">Achternaam</label>
<input type="text" name="surName" id="surName" value="" />
<span class="required">*</span>
<label for="email">E-mail</label>
<input type="email" id="email" name="email" placeholder="example@domain.com" value="" />
<span class="required">*</span>
<input type="submit" name="submit" id="submit">
</form>';
}
これにより、私のページは解析されません。エラー報告をオンにしていますが、エラー以外は何も表示されません
内部サーバー エラー 500
私のコンソールログ(Firebug)