2011-11-14 11 views
0

連絡先ページにHTMLフォームフィールドがあり、それがデータをPHPページに送信します。xhtml連絡フォームフィールドは必須ですか?

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

どうすればいいですか?

<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> 
+0

。 jQueryベースの検証ライブラリを試しましたか? – Rup

答えて

0

フォームをPHPファイル(または.phtmlが推奨*)に入れてください。 .input-errorのようなCSSクラスを追加してください。

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

はフォームがに提出する必要があります:

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

が明確な例を与えるために:あなたのフォームで

.input-error { color: red; } 

は、各フィールドのため、このような何かをする必要がありますフォームを処理してこのページに戻るPHPファイル。

$postData = $_POST; 

あるいは、そのスクリプトが別のページにリダイレクトするならば、あなたはセッションでポストデータを格納する必要があります:そのファイルでは、このような行が必要があると思います。以下のような:(存在する場合)

$_SESSION['postData'] = $_POST; 

が、その場合には、フォームの上部にあるどこかのコントローラでは、そのようにそのデータを取得する:あなたはこのjQueryのをタグ付けしました

$postData = $_SESSION['postData']; 
関連する問題