繁体   English   中英

验证HTML联系人表单

[英]Validate a HTML Contact Form

我有一个HTML联系人表单,需要以某种方式进行验证。

如果没有填写任何字段,而不是用JavaScript提醒用户,我宁愿在保留为空的字段下方显示“必填字段”一词。

这是我的联系表格contact.html

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

<label for="name">Name:</label>
<input type="text" name="name" id="name" value="" tabindex="1" />


<label for="name">Email:</label>
<input type="text" name="email" id="email" value="" tabindex="1" />


<input type="submit" value="Submit" class="button" />
</form>

这是我的PHP表格contact.php (除了验证之外,所有内容都省略了):

//Validation... I am building up an error message string to alert the user at the end (omitted) 

$errormessage = '';

if(!isset($_POST['name']) || strlen($_POST['name']) < 1){
$errormessage .= 'You have not entered your Name\n';
}

if(!isset($_POST['email']) || strlen($_POST['email']) < 1){
$errormessage .= 'You have not entered your Email Address\n';
}

if($errormessage != ''){ ?>

<script language="javascript" type="text/javascript">
    alert('<?php echo "$errormessage" ?>');
    window.location = 'contact.html';
</script>

<?php } 


else {

$mail_to = 'info@email.co.uk';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n\n";
$body_message .= 'E-mail: '.$field_email."\n\n";



$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

}

如您所见,我当前的验证在PHP中建立了一个字符串,该字符串将在最后通知用户(忽略),但是我想通过在HTML字段下方显示字符串“ Field Required”来替换此方法。留空。

如果可以使用某些html5功能,另一种选择是在输入上设置必需的属性。

需要

此属性指定用户在提交表单之前必须填写一个值。 当type属性为隐藏,图像或按钮类型(提交,重置或按钮)时,不能使用它。 :optional和:required CSS伪类将适当地应用于该字段。

取自https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input

因此,您的html将变为:

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

<label for="name">Name:</label>
<input type="text" name="name" id="name" value="" tabindex="1" required />


<label for="name">Email:</label>
<input type="text" name="email" id="email" value="" tabindex="1" required />


<input type="submit" value="Submit" class="button" />
</form>

我想回答您的问题而不偏离您的方法。

如果contact.html可以由php解析,或者只是更改为contactform.php,则可以向其发送查询字符串,以指示哪些字段出错,并在错误下方显示一条消息。

contactform.php

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

<label for="name">Name:</label>
<input type="text" name="name" id="name" value="" tabindex="1" />
<?php if (isset($_GET['name']) && $_GET['name'] == 1) { 
    echo '<br/>This field is required.'; 
} ?>


<label for="name">Email:</label>
<input type="text" name="email" id="email" value="" tabindex="1" />
<?php if (isset($_GET['email']) && $_GET['email'] == 1) { 
    echo '<br/>This field is required.'; 
} ?>

contact.php

$error = FALSE;

$errors = array();

if(!isset($_POST['name']) || strlen($_POST['name']) < 1){
    $error = TRUE;
    $errors[] = 'name=1';
}

if(!isset($_POST['email']) || strlen($_POST['email']) < 1){
    $error = TRUE;
    $errors[] = 'email=1';
}

$geterrors = (count($errors) > 1 ? implode('&',$errors) : $errors[0]);

if($error){ ?>

<script language="javascript" type="text/javascript">
    window.location = 'contactform.php?<?php echo $geterrors; ?>';
</script>

...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM