Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何验证电子邮件地址_Php_Javascript_Ajax - Fatal编程技术网

Php 如何验证电子邮件地址

Php 如何验证电子邮件地址,php,javascript,ajax,Php,Javascript,Ajax,可能重复: 我想知道有人能帮我吗 我需要验证以下代码的电子邮件地址,但我有问题 <?php if ($_POST) { $expected = array('name', 'email', 'emailmessage'); $validation = array( 'name' => 'Please provide your full name', 'email' => 'Please pr

可能重复:

我想知道有人能帮我吗

我需要验证以下代码的电子邮件地址,但我有问题

<?php

if ($_POST) {

    $expected = array('name', 'email', 'emailmessage');
    $validation = array(
        'name'          => 'Please provide your full name',
        'email'         => 'Please provide your valid email address',
        'emailmessage'  => 'Please provide message'
    );

    $errors = array();
    $output = array();

    foreach($expected as $key) {

        $input = htmlspecialchars($_POST[$key]);

        if (array_key_exists($key, $_POST)) {
            if (empty($_POST[$key])) {
                $errors[$key] = $validation[$key];
            } else {
                $output[$key] = $_POST[$key];
            }
        } else {
            $errors[$key] = $validation[$key];
        }

    }

    if (!empty($errors)) {
        $array = array('error' => true, 'fields' => $errors);
    } else {

        // PROCESS FORM

    // ---------------------------------------------------------
    // BEGIN EDITING
    // ---------------------------------------------------------

    $to = "qakbar@hotmail.co.uk"; //This is the email address messages will be sent to
    $web_name = "My Test Web Form"; //This is the name of your website that will show in your email inbox

    //get IP address
    $ip = $_SERVER['REMOTE_ADDR'];

    //make time
    $time = time();
    $date = date("r", $time);

    // ---------------------------------------------------------
    // END EDITING
    // ---------------------------------------------------------

    $emailmessage = trim($emailmessage);
    $emailmessage = nl2br($emailmessage);
    $emailmessage = htmlspecialchars($emailmessage);
    $emailmessage = wordwrap($emailmessage, 70);

    //Visible form elements
    $name = $_POST['name']; //Sender's name
    $email = $_POST['email']; //Sender's email
    $emailmessage = htmlspecialchars($_POST['emailmessage']); //Sender's message

    //Setting up email
    $subject = "New Message from $web_name";

    $message = "
                New message from $name <br/><br/>

                Message:<br />
                $emailmessage
                <br/>

                <br/>
                Email: $email<br />
                IP:</strong> <span style=\"color:#990000;\">$ip</span><br />
                Date:</strong> $date

                ";

    $header  = 'MIME-Version: 1.0' . "\r\n";
    $header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
    $header .= 'From:'. $email . " \r\n";

    $sent = mail($to, $subject, $message, $header);

        //$message = '<div id=message>You have successfully subscribed to our newsletter</div>';
        $array = array('error' => false, 'message' => $message);
    }

    echo json_encode($array);
}
任何帮助都是非常感激的


谢谢

您可以这样使用它:

filter_var($email, FILTER_VALIDATE_EMAIL)  or  die("Email wrong.");
就在您指定了这个确切的变量之后:

$email = $_POST['email'];

当然,这种结构可以更合理,也可以有更好的错误通知。但听起来似乎您首先需要更多的PHP通用实践。

您的代码有一个奇怪之处:

    $input = htmlspecialchars($_POST[$key]);

    if (array_key_exists($key, $_POST)) {

在检查密钥是否存在之前,您已经在使用该密钥。另外,$input变量在代码中不再使用,因此它是一个无用的行。

可能是,此代码将帮助您。试试看

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
    $name = $_POST['uname'];
    $email = $_POST['email'];

    $valid_arr = array();
    $error_arr = array();

        if($name == ''){
            $error_arr['name'] = 'Required';
        }
        else if(!preg_match('/^[a-zA-A]+$/',$name)){
            $error_arr['name'] = 'Please put correct value';
        }
        else{
            $valid_arr['name'] = $name;
        }

        if($email == ''){
                $error_arr['email'] = 'Required';
            }
            else if(!preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/',$email)){
                $error_arr['email'] = 'Exm.- john@gmail.com';
            }
            else{
                $valid_arr['email'] = $email;
            }

            if(count($error_arr) == 0){
                header('location: success.php');
            }
            else{
                echo 'Error in Loading';
            }
}
?>
<html>
<head>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="POST">
    <table>
    <tr>
        <td><label>User Name :</label></td>
        <td><input type="text" name="uname" value="<?php echo $valid_arr['name'];?>"/></td>
        <td class="error"><?php echo $error_arr['name'];?></td>
    </tr>
    <tr>
        <td><label>Email :</label></td>
        <td><input type="text" name="email" value="<?php echo $valid_arr['email'];?>"/></td>
        <td class="error"><?php echo $error_arr['email'];?></td>
    </tr>
    <tr>
        <td><input type="submit" name="save" value="Submit"/></td>
    </tr>
    </table>
</form>
</body>
</html>
多个副本:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
    $name = $_POST['uname'];
    $email = $_POST['email'];

    $valid_arr = array();
    $error_arr = array();

        if($name == ''){
            $error_arr['name'] = 'Required';
        }
        else if(!preg_match('/^[a-zA-A]+$/',$name)){
            $error_arr['name'] = 'Please put correct value';
        }
        else{
            $valid_arr['name'] = $name;
        }

        if($email == ''){
                $error_arr['email'] = 'Required';
            }
            else if(!preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/',$email)){
                $error_arr['email'] = 'Exm.- john@gmail.com';
            }
            else{
                $valid_arr['email'] = $email;
            }

            if(count($error_arr) == 0){
                header('location: success.php');
            }
            else{
                echo 'Error in Loading';
            }
}
?>
<html>
<head>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="POST">
    <table>
    <tr>
        <td><label>User Name :</label></td>
        <td><input type="text" name="uname" value="<?php echo $valid_arr['name'];?>"/></td>
        <td class="error"><?php echo $error_arr['name'];?></td>
    </tr>
    <tr>
        <td><label>Email :</label></td>
        <td><input type="text" name="email" value="<?php echo $valid_arr['email'];?>"/></td>
        <td class="error"><?php echo $error_arr['email'];?></td>
    </tr>
    <tr>
        <td><input type="submit" name="save" value="Submit"/></td>
    </tr>
    </table>
</form>
</body>
</html>