javascript - 在 php 表单提交之前添加电子邮件验证

标签 javascript php jquery json forms

我有以下简单的表单,我正在尝试将电子邮件验证错误发送到 在提交之前显示在表单中以显示错误。

有没有办法用 PHP 来做到这一点还是我必须使用 JSON?

如果我必须使用 JSON,谁能告诉我该怎么做?

提前致谢。

表单.html:

<form method="post" name="form" action="form.php">
<p>Robot: <input type="text" name="robot" ></p> 
<p>Name: <input type="text" name="name" ></p>
<p>Email:    <input type="email" name="email"></p>
<p>Phone:    <input type="telephone" name="phone"></p>
<p>Message:  <textarea name="message"></textarea></p>
<p><input type="submit" value="Send Form"></p>
</form>
<div id="error"></div>

表单.php

<?php

// send to and from
$to = "email@example.com";
$headers = "From: email@example.com \r\n";
$headers .= "Reply-To: email@example.com \r\n";

// form inputs
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$robot = $_POST['robot'];

// email message
$email_subject = "Web Contact Message";

$email_body =   
"A message from your website contact form \n\n".
"Email: $email \n\n".
"Phone: $phone \n\n".
"From: $name \n\n".
"Message: \n". 
"$message \n";

// honeypot
if($robot)
header( "Location: http://www.example.com/nothankyou.html" );
    else{

//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
 {
 echo '<div id="error">Please Enter a Valid Email</div>';
 }
else
 {

// send it
 mail($to,$email_subject,$email_body,$headers); 
 header( "Location: http://www.example.com/thankyou.html" );

 }  
 }   
?>

最佳答案

您可以尝试使用 javascript/jquery 插件进行前端验证(例如 http://jqueryvalidation.org/http://bootstrapvalidator.com/ )。如果您仍然想保留现有代码,我建议如下:

首先,将 form.html 合并到 form.php 确保您的 php 邮件代码位于文件顶部,因为 php header 在调用它们之前无法完成任何输出。

    <?php
if (count($_POST)) {

// send to and from
    $to = "email@example.com";
    $headers = "From: email@example.com \r\n";
    $headers .= "Reply-To: email@example.com \r\n";

// form inputs
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $robot = $_POST['robot'];

// email message
    $email_subject = "Web Contact Message";

    $email_body = "A message from your website contact form \n\n" .
            "Email: $email \n\n" .
            "Phone: $phone \n\n" .
            "From: $name \n\n" .
            "Message: \n" .
            "$message \n";

// honeypot
    if ($robot)
        header("Location: http://www.example.com/nothankyou.html");
    else {

//validate email
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            header("Location: form.php?error=email_error");
        } else {

// send it
            mail($to, $email_subject, $email_body, $headers);
            header("Location: http://www.example.com/thankyou.html");
        }
    }
}
?>
<form method="post" name="form" action="">
    <p>Robot: <input type="text" name="robot" ></p> 
    <p>Name: <input type="text" name="name" ></p>
    <p>Email:    <input type="email" name="email"></p>
    <p>Phone:    <input type="telephone" name="phone"></p>
    <p>Message:  <textarea name="message"></textarea></p>
    <p><input type="submit" value="Send Form"></p>
</form>

<?php
if (isset($_GET["error"]) && $_GET["error"] == "email_error") {
    ?>
    <div id="error">Please Enter a Valid Email</div>
    <?php
} 

关于javascript - 在 php 表单提交之前添加电子邮件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25225429/

相关文章:

javascript - 使用 Javascript/jQuery 或 PHP 解析 HTML

php - WordPress - 在 "get_template_part();"之前插入空行

javascript - 如何一次获取一页数据

javascript - 获取复选框值并将它们放入数组中

javascript - 未捕获( promise )TypeError : Cannot read property 'style' of null

javascript - 了解 getElementById 等

javascript - 如何获取 Dom 对象的内容包括自身?

javascript - 如何在 jquery 中递归地将所有子元素传输到组 div 包装器?

php - getJSON - 为什么它不起作用?

jquery - 片段 - 淡入然后淡出