我想通过电子邮件验证表单。我不知道我的代码是否合适。 我不知道把所有的东西放在一起做一些可能与嵌套if。 我想做这个: 1)如果用户名,密码或地址为空,则为每个用户提供回音 是否有效以及其他无效的回声 2)当电子邮件地址拼写错误时给出回声,如果它是正确的另一个回声。 3)如果电子邮件地址为空以给出回声而对于另一回声则为假 4)与用户相同,密码分别为每个空,2个回一个为真,另一个为假。 请帮忙!
<html>
<head></head>
<body>
<form action ="form_testare1.php" method = "POST">
User:<input type="text" name = "user" />
<br/>
password:<input type = "password" name = "password" />
<br/>
Email:<input type = "text" name = "email" />
<br/>
<input type = "submit" name = "submit" value ="submit"/>
</form>
<?php
$password = $_POST['password'];
$user = $_POST['user'];
$email = $_POST['email'];
if(isset($_POST['user']) && ($_POST['password']) && ($_POST['email']))
{
echo "user , password and email are set<br/>";
}
else
{
echo "user:$user password:$password or email:$email not set<br/>";
}
if(strlen($password)>0 && strlen($user)>0 && strlen($email)>0){
echo "User: $user<br/>";
echo "Password: $password<br/>";
echo "Email: $email<br/>";
}
else
{
echo "user:$user password:$password or email:$email are empty<br/>";
}
if(filter_var($email,FILTER_VALIDATE_EMAIL))
{
echo "emailul is valid";
}
else
{
echo "emailul is not valid<br/>";
}
?>
</body>
</html>
答案 0 :(得分:1)
要简短,您可以将输入命名为data[name]
,data[email]
...
并检查这样
foreach($_POST['data'] as $key=>$value){
if($key == 'email'){
if(!empty($value) && preg_match("/^(.*)@(.*)\.(.*)$/",$value)){
echo 'email is valid';
}else{
echo 'email is invalid';
}
}else{
echo !empty($value)?"$key is correct":"$key is incorrect";
}
}
答案 1 :(得分:0)
之前必须纠正
if(isset($_POST['user']) && isset($_POST['password']) && isset($_POST['email']))
答案 2 :(得分:0)
这样改名?
<form action ="form_testare1.php" method = "POST">
User:<input type="text" name = data['user'] />
<br/>
password:<input type = "password" name = data['password'] />
<br/>
Email:<input type = "text" name = data['email'] />
<br/>
<input type = "submit" name = "submit" value ="submit"/>
</form>