将PHP电子邮件验证添加到变量样式的代码中 [英] Add PHP email validation to a variable styled code

查看:114
本文介绍了将PHP电子邮件验证添加到变量样式的代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我找到了这段代码来验证电子邮件:

Simply, I found this code to validate email:

filter_var($ user_email,FILTER_VALIDATE_EMAIL)

但是不幸的是,我不知道如何将其添加到我的PHP代码中,以便从表单中获取信息:

But unfortunately I don't know how to add this to my PHP code that get info from the form:

<?php $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "example@example.com";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>

我使用Ajax来获取表单的提交信息。

I used Ajax to get form's submitted information.

推荐答案

您可以使用 filter_input 函数。此函数将检查 email 参数是否已在 POST 请求中发送,如果已经发送,则它将进行验证并返回经过验证的电子邮件或FALSE。这样做:

You can use filter_input function. This function will check if email argument has been sent in the POST request and if it has been, then it will validate it and return validated email or FALSE. Do it like this:

<?php 
    $email = filter_input( INPUT_POST, 'email', FILTER_VALIDATE_EMAIL );
    if ( $email ) {
        $name = $_POST['name'];
        $message = $_POST['message'];
        $formcontent="From: $name \n Message: $message";
        $recipient = "example@example.com";
        $subject = "Contact Form";
        $mailheader = "From: $email \r\n";
        mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    } else {
        die("Email is ivalid!")
    }
?>

这篇关于将PHP电子邮件验证添加到变量样式的代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆