0

連絡先ページにHTMLフォームフィールドがあり、データをphpページに送信します。

フォームフィールドを必須にし、ユーザーが値を入力しない場合はラベルの横に赤(*)を表示したいと思います。

これどうやってするの?

<form id="contact_form" method="post" action="contact.php">

    <p style="margin-top:20px">
        <label for="title">Title</label><br/>
        <input id="your_name" name="your_name" type="text" style="width:94%"/>
    </p>

    <p style="margin-top:20px">
        <label for="initial">Initial</label><br/>
        <input id="initial" name="initial" type="text" style="width:94%"/>
    </p>
    <p style="margin-top:20px">
        <label for="surname">Surname</label><br/>
        <input id="surname" name="surname" type="text" style="width:94%"/>
    </p>
        <p style="margin-top:20px">
        <label for="tel_number">Tel number</label><br/>
        <input id="tel_number" name="tel_number" type="text" style="width:94%"/>
    </p>
    <p style="margin-top:20px">
        <label for="email">Email</label><br/>
        <input id="email" name="email" type="text" style="width:94%"/>
    </p>

    <p style="margin-top:20px">
        <label for="enquiry">Enquiry</label><br/>
        <textarea id="enquiry" name="enquiry" rows="7" cols="10" style="width:94%"></textarea>
    </p>

    <p style="margin-top:50px">
        <input type="submit" value="Send Message"/><br/>
    </p>

</form>
4

1 に答える 1

0

フォームをphpファイルに入れる必要があります(または.phtml推奨*)。のようなcssクラスを追加し.input-errorます。

.input-error { color: red; }

フォームでは、各フィールドに次のようなものが必要です。

if (empty($postData['field']) {
    echo "<span class=\"input-error\">*</span>";
}

明確な例を挙げると:

<p style="margin-top:20px">
    <?php if (empty($postData['your_name']): ?>
        <span class="input-error">*</span>
    <?php endif; ?>
    <label for="title">Title</label><br/>
    <input id="your_name" name="your_name" type="text" style="width:94%"/>
</p>

フォームは、フォームを処理してこのページに戻る php ファイルに送信する必要があります。そのファイルには、次のような行が必要です。

$postData = $_POST;

または、そのスクリプトが別のページにリダイレクトする場合は、投稿データをセッションに保存する必要があります。お気に入り:

$_SESSION['postData'] = $_POST;

その場合、フォームの上部またはコントローラーのどこか (存在する場合) で、次のようにデータを取得します。

$postData = $_SESSION['postData'];
于 2011-11-14T10:50:04.510 に答える