【问题标题】:Form Email Validation in backend PHP后端 PHP 中的表单电子邮件验证
【发布时间】:2015-02-27 22:40:15
【问题描述】:

我正在使用表单在我的网站上注册时事通讯。我正在使用一个运行良好但没有验证的 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");
}


?>

这不起作用。我认为这是因为我在错误的地方实现了代码,但我不确定。任何帮助将不胜感激。

【问题讨论】:

    标签: php jquery ajax forms validation


    【解决方案1】:

    您可以在 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

    【讨论】:

      【解决方案2】:

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

      代码如下所示

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

      【讨论】:

      • 我正在尝试:jQuery(document).ready(function(){ $("#contactform").validate({ rules: { email: { required: true. } }, messages: {电子邮件:“必填字段”} }); });但我得到一个 TypeError: undefined is not a function (evalating '$("#contactform").validate')
      【解决方案3】:

      您可以通过使用此代码来使用服务器端验证

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

      【讨论】:

        猜你喜欢
        • 2013-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-09
        • 1970-01-01
        • 1970-01-01
        • 2018-01-26
        • 2015-11-13
        相关资源
        最近更新 更多