Javascript xhtml必需的联系人表单字段?

Javascript xhtml必需的联系人表单字段?,javascript,jquery,html,xhtml,contact-form,Javascript,Jquery,Html,Xhtml,Contact Form,我在联系人页面上有HTML表单字段,然后将数据发送到php页面 如果用户未输入值,我希望表单字段为必填字段,并在标签旁边显示红色(*) 我该怎么做 <form id="contact_form" method="post" action="contact.php"> <p style="margin-top:20px"> <label for="title">Title</label><br/>

我在联系人页面上有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>

标题

首字母

姓氏

电话号码

电子邮件

查询



您必须将表单放入php文件中(或
.phtml
推荐*)。添加一个css类,如
。输入错误

.input-error { color: red; }
在表单中,每个字段都需要类似的内容:

if (empty($postData['field']) {
    echo "<span class=\"input-error\">*</span>";
}
或者,如果该脚本重定向到另一个页面,则需要在会话中存储post数据。比如:

$_SESSION['postData'] = $_POST;
在这种情况下,在窗体顶部或控制器中的某个位置(如果有),按如下方式检索该数据:

$postData = $_SESSION['postData'];

您已经标记了这个jQuery。您是否尝试过基于jQuery的验证库?
$_SESSION['postData'] = $_POST;
$postData = $_SESSION['postData'];