PHP フォーム検証 - フォームインスタンスの完成

このセクションでは、ユーザーがフォームをサブミットした後に入力フィールドの値を保持する方法を説明します。

PHP - フォームの値を保持

ユーザーがサブミットボタンをクリックした後に入力フィールドに値を表示する場合、以下の入力フィールドの value 属性に PHP スクリプトを追加しました:name、email そして website。comment テキストボックスフィールドでは、スクリプトを <textarea> と </textarea> 之间に配置しました。これらのスクリプトは $name、$email、$website そして $comment 変数の値を出力します。

次に、どのラジオボタンが選択されたかを表示する必要があります。これには、checked属性(ラジオボタンのvalue属性ではなく)を操作する必要があります:

名前: <input type="text" name="name" value="<?php echo $name;?>">
Eメール: <input type="text" name="email" value="<?php echo $email;?>">
ウェブサイト: <input type="text" name="website" value="<?php echo $website;?>">
コメント: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
性別:
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="female") echo "checked";?>
value="female">女性
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="male") echo "checked";?>
value="male">男性

PHP - 完全なフォーム例

以下は PHP フォーム検証の完全なコードです:

実行例