后端PHP中的表单电子邮件验证
我正在使用表格在我的网站上注册时事通讯。我正在使用一个contact.php文件,该文件运行良好,但没有验证,因此我偶尔会收到空白响应 我不确定这是为什么,但我相信我需要验证 这是我的原始代码后端PHP中的表单电子邮件验证,php,jquery,ajax,forms,validation,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 informa
<?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 = "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中使用filter\u 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;
}
if(过滤变量($email,过滤验证电子邮件)){
mail($to,$subject,$message,“From:“.$email.”;//一个非常简单的发送
echo'contactarea |谢谢。我们保证您不会后悔。;//现在让我们更新contact.html页面上的“contactarea”div。contactarea |告诉javascript要更新哪个div。
}
否则{
$errormsg.=“
”;
$error++;
回显“错误|”。$errormsg;
}
如果您想了解更多信息,请访问这里的官方PHP文档页面:或使用jquery验证插件。我极力推荐
代码如下所示
$( "#myform" ).validate({
rules: {
field: {
required: true,
email: true
}
}
});
您可以使用以下代码使用服务器端验证
if (filter_var($email, FILTER_VALIDATE_EMAIL) === true) {
//your email sending code here
} else {
echo("$email is not a valid email address");
}
我正在尝试:jQuery(document).ready(function(){$(“#contactform”).validate({rules:{email:{required:true.}},messages:{email:{required Field}});});但是我得到一个类型错误:undefined不是一个函数(计算“$”(“#contactform”).validate“)
$( "#myform" ).validate({
rules: {
field: {
required: true,
email: true
}
}
});
if (filter_var($email, FILTER_VALIDATE_EMAIL) === true) {
//your email sending code here
} else {
echo("$email is not a valid email address");
}