我有一个列表框,其中有两个值"Student"answers"Business"。我还有一个电子邮件输入框和一个提交按钮。当用户选择"学生"时,他们应该只被允许输入一个带有@mail.ITname.ie的电子邮件地址。当选择业务时,它应该只检查有效的电子邮件。我真的是php的新手,发现很难找到教程来做我想要做的事情。感谢您的帮助。
<?php
if(isset($_POST['email']) == true && empty($_POST['email']) == false){
$email = $_POST['email'];
$allowed = array('mail.ITname.com');
if (filter_var($email, FILTER_VALIDATE_EMAIL) == true)
{
$domain = array_pop(explode('@', $email));
if ( ! in_array($domain, $allowed))
{
// Not allowed
}
}
}
?>
<form action="" method="post">
<select name="user type" size="2" >
<option>Student</option>
<option>Business</option>
</select>
<br />
<input type="text" name="email" />
<input type="submit" />
</form>
首先将您的<select>
名称更改为如下(无空格):
<select name="user_type" size="2" >
然后您可以检查$_POST['user_type']
服务器端,并适当地处理验证。
它最终可能会看起来像这样:
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if($_POST['user_type'] == 'Student') {
// Validate the domain
$domain = array_pop(explode('@', $email));
if (in_array($domain, $allowed)) {
// Email is valid.
}
} else {
// User must be 'Business', so we're done. Email is valid.
}
}
// Validation failed somewhere. Display an error? Redirect?