2013-11-26 42 views
0

我需要幫助。我需要做什麼來驗證此表單中的電子郵件?這是一個登錄頁面,我想在訪問我的內容here is this demo之前獲取訪問者的電子郵件。HTML表單工作,但需要php驗證

我可以得到一些提示或正確的代碼用於此頁面嗎?

<form id="contact-form" action="send.php"> 
<input type="text" name="email" placeholder="[email protected]" class="cform-text" size="40" title="your email" required> 
<input type="submit" value="Enter Now" class="cform-submit"> 
</form> 

send.php文件:

$visitorEmail = $_GET['email']; 
$email_from = "[email protected]"; 
$email_subject = "New Form Submission! - From: " + $visitorEmail; 
// edit here 
$email_body = "New visitor - $visitorEmail";  
$to = "[email protected]"; 
$headers = "From: $email_from \r \n"; 
$headers .= "Reply-To: $visitorEmail \r \n";  
mail($to, $email_subject, $email_body, $headers); 
header("Location: http://www.de-signs.com.au"); 

謝謝!

+1

http://www.sitepoint.com/form-validation-with-php/ – AdRock

回答

1

使用PHP的filter_var()功能:

if(filter_var($visitorEmail, FILTER_VALIDATE_EMAIL) === false) { 
    // Invalid E-Mail address 
} else { 
    // OK 
} 

所以到實體模型使用新的腳本。它會是這個樣子

$visitorEmail = $_GET['email']; 

if(filter_var($visitorEmail, FILTER_VALIDATE_EMAIL) === false) { 
    die('Sorry that\'s no valid e-mail address'); 
} else { 
    $email_from = '[email protected]'; 
    $email_subject = 'New Form Submission! - From: ' . $visitorEmail; 

    // edit here 
    $email_body = 'New visitor - ' . $visitorEmail;  
    $to = '[email protected]'; 
    $headers = 'From: $email_from' . PHP_EOL; 
    $headers .= 'Reply-To: $visitorEmail' . PHP_EOL;  
    mail($to, $email_subject, $email_body, $headers); 

    header('Location: http://www.de-signs.com.au'); 
    exit; 
} 
+0

不工作.... http://www.de-signs.com.au/test/ – user3036585

+0

<? php $ visitorEmail = $ _GET ['email']; if(filter_var($ visitorEmail,FILTER_VALIDATE_EMAIL)=== false){ die('抱歉,沒有有效的電子郵件地址');其他{ } $ email_subject ='提交! - 來自:'。 $ visitorEmail; $ email_body ='新訪問者 - 。 $ visitorEmail「; $ to ='[email protected]'; $ headers ='From:$ email_from'。 PHP_EOL; $ headers。='回覆:$ visitorEmail'。 PHP_EOL; 郵件($ to,$ email_subject,$ email_body,$ headers); header('Location:http://www.de-signs.com.au'); \t \t exit; \t?> – user3036585

+0

'/ home2/designsc/public_html/test/send.php'中的'19'是什麼? – TiMESPLiNTER

0

HTML5的新email類型輸入爲您提供幫助。

<input type="email" name="email" placeholder="[email protected]" class="cform-text" size="40" title="your email" required />