2012-01-10 114 views
1
<?php 
if (isset($_POST['ign'], $_POST['email'])) { 
    if($_POST['ign'] && $_POST['email']){ 

    } 
    else { 
     echo ("Please enter all of the values!"); 
    } 
} 
else { 
    echo ("Error in form data!"); 
} 
if((FILTER_VALIDATE_EMAIL($_POST['email'] == TRUE))) { 
    $email = $_POST['email']; 
    echo ("Thanks, " . htmlentities($_POST['ign']) . ", you will recieve an email when the site is complete!"); 
} 
else { 
    echo "Failure!"; 
} 

// insert email and ign into database 
?> 

这会正常工作吗?第一次完全从头开始做一些事情!验证电子邮件错误

行!我改变了它。那这个呢?我还应该做空吗?

<?php 
if (!isset($_POST['ign'], $_POST['email'])) { 
    if($_POST['ign'] && $_POST['email']){ 
    echo "Please fill out all of the fields!"; 
     die; 
} 
if(var_filter($_POST['email'], FILTER_VALIDATE_EMAIL)) 
    $email = $_POST['email']; 
    echo ("Thanks, " . htmlentities($_POST['ign']) . ", you will recieve an email when the site is complete!"); 
} 
else { 
    echo "Your email was invalid!"; 
} 

// insert email and ign into database 
?> 
+0

+1用于查看第一次尝试验证的过滤器函数! – cmbuckley 2012-01-10 00:49:42

+0

不,它不会起作用。但在你问它是否确实如此之前,你应该执行代码并尝试修复它返回的错误。了解错误并修复它们会大大帮助您未来的编程。 – fivedigit 2012-01-10 00:49:58

+0

[if!isset multiple OR conditions]的可能重复(http://stackoverflow.com/questions/8784584/if-isset-multiple-or-conditions) – 2012-01-10 00:54:07

回答

0

使用内置的功能,不要重新发明轮子:

if(filter_var($mail, FILTER_VALIDATE_EMAIL)){ 
    echo "Mail is valid!"; 
} 

为什么你的函数名全部大写?

...和你看到这之间的区别...

if(func($_POST['email'] == TRUE)){ 

这..

if(func($_POST['email']) == TRUE){ 

+0

**今晚可能还会有20个以上的问题**警告单词**:这将验证诸如'foo @ example'(无TLD)之类的内容,因为它是有效的(尽管是本地)电子邮件地址。看看结合[chkdnsrr](http://php.net/manual/en/function.checkdnsrr.php)来验证地址的域部分。 – cmbuckley 2012-01-10 00:53:31

+0

不,大声笑。我也认为FILTER_VALIDATE_EMAIL是它的功能...我没有使用filter_var。 – Depetrify 2012-01-10 00:55:02

+0

@ user1138090:查看括号的顺序。他们有微妙的不同。 – cmbuckley 2012-01-10 01:02:53

0

那里有很多错误。以下是您应该做的事情:

// First check if both fields are present. Usually there is no point in doing this 
// because the next check will also catch this case, but you had it so I put it in too. 
if (!isset($_POST['ign'], $_POST['email'])) { 
    echo ("Error in form data!"); 
    die; // or something else 
} 

// Then check if both values are non-"empty" (you might want to look at the docs for 
// an explanation of what this means exactly). 
if (empty($_POST['ign']) || empty($_POST['email'])) { 
    echo ("Please enter all of the values!"); 
    die; // or something else 
} 

// Finally, validate the email. DO NOT COMPARE WITH true! 
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 
    echo "Failure!"; 
    die; // etc 
} 

echo ("Thanks, " . htmlentities($_POST['ign']) . ", blah blah!"); 
+0

OP的初始isset()调用完全有效。看起来这个学生已经教了关于isset()函数签名的主人(http://php.net/manual/en/function.isset.php):-D – cmbuckley 2012-01-10 00:57:05

+0

@cbuckley:糟糕。那么,你每天都会学到一些东西!编辑答案,以免听起来很愚蠢。 ;-) – Jon 2012-01-10 01:02:16