在后端PHP中形成电子邮件验证

时间:2015-02-27 22:40:15

标签: php jquery ajax forms validation

我正在使用表单在我的网站上获取简报注册。我使用的contact.php文件运行良好,但没有验证,所以我偶尔也会经常得到空白回复。

我不确定为什么会这样,但我相信我需要验证。

这是我的原始代码

<?php
/*

Author: Andrew Walsh
Date: 30/05/2006
Codewalkers_Username: Andrew


This script is a basic contact form which uses AJAX to pass the information to php, thus making the page appear to work without any refreshing or page loading time.

*/

$to = "hello@interzonestudio.com"; //This is the email address you want to send the email to
$subject_prefix = ""; //Use this if you want to have a prefix before the subject

if(!isset($_GET['action']))




{
die("You must not access this page directly!"); //Just to stop people from visiting contact.php normally
}

/* Now lets trim up the input before sending it */


$subject = "Newsletter Sign Up"; //The senders subject
$message = trim($_GET['email']); //The senders subject
$email = trim($_GET['email']); //The senders email address

mail($to,$subject,$message,"From: ".$email.""); //a very simple send

echo 'contactarea|Thank you. We promise you won’t regret it.'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update.
?>

这是我尝试添加以验证的代码,但它不起作用。

<?php
/*

Author: Andrew Walsh
Date: 30/05/2006
Codewalkers_Username: Andrew


This script is a basic contact form which uses AJAX to pass the information to php, thus making the page appear to work without any refreshing or page loading time.

*/

$to = "jcash1@gmail.com"; //This is the email address you want to send the email to
$subject_prefix = ""; //Use this if you want to have a prefix before the subject

if(!isset($_GET['action']))




{
die("You must not access this page directly!"); //Just to stop people from visiting contact.php normally
}

/* Now lets trim up the input before sending it */


$subject = "Newsletter Sign Up"; //The senders subject
$message = trim($_GET['email']); //The senders subject
$email = trim($_GET['email']); //The senders email address


/* Validation */
$error=0; // check up variable
$errormsg = '<ul class="errorlist">';

/* get it checking */


if(!check_email($email))
{
  $errormsg.=  "<li class='errormessage'>ERROR: not a valid email.</li>";
  $error++;
}

$errormsg .= '</ul>';


if($error == 0) {


mail($to,$subject,$message,"From: ".$email.""); //a very simple send

echo 'contactarea|Thank you. We promise you won’t regret it.'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update.

 } else { 
    echo 'error|'. $errormsg;
 }

?>

任何人都可以提供一些见解吗?


我不能为我的生活让这个工作...... 我收到插件错误,我已正确加载

所以我尝试添加这个:

if (filter_var($email, FILTER_VALIDATE_EMAIL) === true) {
  //your email sending code here
} else {
  echo("$email is not a valid email address");
}
像这样:

<?php
/*

Author: Andrew Walsh
Date: 30/05/2006
Codewalkers_Username: Andrew


This script is a basic contact form which uses AJAX to pass the information to php, thus making the page appear to work without any refreshing or page loading time.

*/

$to = "hello@interzonestudio.com"; //This is the email address you want to send the email to
$subject_prefix = ""; //Use this if you want to have a prefix before the subject

if(!isset($_GET['action']))




{
die("You must not access this page directly!"); //Just to stop people from visiting contact.php normally
}

/* Now lets trim up the input before sending it */

if (filter_var($email, FILTER_VALIDATE_EMAIL) === true) {



$subject = "Newsletter Sign Up"; //The senders subject
$message = trim($_GET['email']); //The senders subject
$email = trim($_GET['email']); //The senders email address


mail($to,$subject,$message,"From: ".$email.""); //a very simple send



echo 'contactarea|<div id="thanks">Thank you. We promise you won’t regret it.</div>'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update.

} else {
  echo("$email is not a valid email address");
}


?>

哪个不行。我认为我已经在错误的地方实现了代码,但我不确定。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

您可以在PHP中使用filter_var()功能来验证电子邮件地址。

要简单验证PHP中的电子邮件地址,您可以像这样使用它,

if(filter_var($email, FILTER_VALIDATE_EMAIL)){
  echo "Valid email";
}

您的代码可以像这样改进。

if(filter_var($email, FILTER_VALIDATE_EMAIL)){
  mail($to,$subject,$message,"From: ".$email.""); //a very simple send
  echo 'contactarea|Thank you. We promise you won’t regret it.'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update.
}
else {
  $errormsg.=  "<li class='errormessage'>ERROR: not a valid email.</li>";
  $error++;
  echo '</ul> error|'. $errormsg;
}

如果您想了解更多信息,请访问官方PHP文档页面:http://php.net/manual/en/filter.filters.validate.php

答案 1 :(得分:0)

或者使用jquery验证插件。我强烈推荐它。

代码看起来类似于下面的

  $( "#myform" ).validate({
   rules: {
   field: {
  required: true,
  email: true
    }
   }
  });

答案 2 :(得分:0)

您可以使用此代码

来使用服务器端验证
if (filter_var($email, FILTER_VALIDATE_EMAIL) === true) {
  //your email sending code here
} else {
  echo("$email is not a valid email address");
}