2012-01-10 87 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