【问题标题】:PHP Email form validating incorrectlyPHP电子邮件表单验证不正确
【发布时间】:2015-11-13 10:27:35
【问题描述】:

我的页面上有一个评论/问题表单。在我实现“提交”代码之前,它会检查验证工作。现在,每次我点击提交时,它都会进入我的“已发送消息”页面,即使我知道它不会分页验证。

我已经检查了一些相关的答案,但无法得到这个答案...

*注意:删除了一些值以达到重点

主要PHP:

<section style="-webkit-flex-direction: column; flex-direction: column;">

            <?php include '/home/ubuntu/workspace/commentform.php';?>

            <h2>Questions &amp; Concerns</h2><br>
            <p><span class="error">* required field</span></p><br>

            <form name="contactform" method="post" action="comment-handler.php">
                Full Name: <input type="text" name="name" value="<?php echo $name;?>" style="width:22em">
                <span class="error">* <?php echo $nameErr;?></span><br><br>
                Phone Number: (xxx) xxx-xxxx <input type="text" name="phone" value="<?php echo $phone;?>" style="width:9.5em"><br><br>
                E-mail: <input type="text" name="email" value="<?php echo $email;?>" style="width: 25em">
                <span class="error">* <?php echo $emailErr;?></span><br><br>

                Comment:<br><sub>*500 Character Maximum.</sub>
                <textarea name="comment" size="500" maxlength="500" rows="10" column="50" style="width: 50em; height: 10em"><?php echo $comment;?></textarea><br><br>
                <input type="submit" name="submit" value="Submit">
            </form>

        </section>

错误处理表单

<?php
$nameErr = $phoneErr = $emailErr = $invoiceErr = "";
$name  = $phone = $email = $invoice = $response = $comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Name is required";
    } else {
        $name = test_input($_POST["name"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
        $nameErr = "Only letters and white space allowed"; 
        }
    }

    if (empty($_POST["phone"])) {
        $phone = "";
    } else {
        $phone = test_input($_POST["phone"]);
    }

    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email format"; 
        }
    }
if (empty($_POST["comment"])) {
        $comment = "";
    } else {
        $comment = test_input($_POST["comment"]);
    }
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

Comment-Handler(提交动作)表单:

<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$invoice = $_POST['invoice'];
$callResponse = $_POST['callResponse'];
$textResponse = $_POST['textResponse'];
$emailResponse = $_POST['emailResponse'];
$comment = $_POST['comment'];
$content = "Name: $name \n\nPhone: $phone \n\nEmail: $email \n\nInvoice #: $invoice \n\nContact Preference(s):\nCall: $callResponse   Text: $textResponse   Email: $emailResponse \n\n Question/Concern:\n\n $comment";



ini_set('display_errors',1);
if(isset($_POST['submit'])){
$to = '7servicerepairs@gmail.com';
$subject = "New Question For Se7en Service!";
$txt = $content;
$headers = array("From: info@se7enservice.com", "Reply-To: $email");
$headers = implode("\r\n", $headers);

mail($to,$subject,$txt,$headers);

    if(mail($to,$subject,$txt,$headers)) {
        include '/home/ubuntu/workspace/sentMessage.php';

    } else {
        echo '<p>Message DID NOT Sent. Please Try Again.</p>';
    }
}
?>

【问题讨论】:

  • 您希望在单击提交按钮和请求发送到服务器之间,还是在请求发送到服务器之后进行验证?
  • 那么它什么时候验证或根本不验证? Error Handling Form 在哪里使用?
  • Mike - 在单击提交按钮和将请求发送到服务器之间。 @chris85 目前,它根本没有验证。在我创建comment-handler.php 并将表单的操作设置为等于它之前,它已经完成了。在表单操作等于 "" 之前,验证工作,出现错误时刷新屏幕,并显示哪些输入有错误。
  • 所以commentform.php == Error Handling Form?如果是这样,那就是问题所在。 PHP 是服务器端,因此它仅在页面加载时可用。当您单击提交时,它会转到comment-handler.php,因此您的验证规则永远不会运行。您可以进行 ajax 检查,在实际提交之前将表单提交到commentform.php。 (您仍然需要服务器端检查,以避免绕过 ajax 的恶意用户)。
  • 好的,我该如何解决。以前从未使用过ajax检查。你能解释一下吗?或者我结合commenform.php和comment-handler.php,我如何组织它以确保表单不提交,除非它通过所有验证规则?

标签: php html forms validation form-submit


【解决方案1】:

这应该可以解决问题...如果我遗漏了什么,请告诉我?或者您需要帮助理解它?

预览:

//PHTML

<?php
if(!empty($_POST)){ 
    $POST = filter_post($_POST);
    $MSG = check_empty($POST);
    if(!array_filter($MSG)){
        if(send_mail($POST)){
            $MSG[] = "Email Success";
        }
        else{
            $MSG[] = "Email Failed";
        }
    }
}
function filter_post($POST){
    $keys = array('name','phone','email','comments');
    $POST = array_intersect_key($POST, array_flip($keys));  
    $POST = array_map('strip_tags', $POST); 
    return($POST);
}
function check_empty($POST){
    foreach($POST as $key => $value){
        if(empty($value)){
            $MSG[] = "You need to fill out the $key section";
        }
    }
    return($MSG);
}
function send_mail($POST){
    extract($POST);
    $to = '7servicerepairs@gmail.com';
    $sbj = 'New Question For Se7en Service!';       
    $msg = "Name = $name \n Phone = $phone \n Email = $email \n Comments = $comments";
    $headers = "From: $email";
    return(mail($to, $sbj, $msg, $headers));
}
?>
<!DOCTYPE html>
<html>
<head>
    <link href="index.css" rel="stylesheet">
</head>
<body>
    <form method="POST" action="">
        <input type="text" name="name" placeholder="Name" value="<?php echo $_POST['name']; ?>">
        <input type="tel" name="phone" placeholder="Phone Number" value="<?php echo $_POST['phone']; ?>">
        <input type="email" name="email" placeholder="Email" value="<?php echo $_POST['email']; ?>">
        <textarea name="comments" maxlength="500" rows="10" cols="10" placeholder="Please enter your comments here..."></textarea>
        <button type="submit">Submit</button>
    </form>             
    <mark><?php echo $MSG[0]; ?></mark>
</body>
</html>

//CSS

body{
    margin: 0 !important;
    width: 100vw;
    height: 100vh;
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: center;
    justify-content: center;
    -wekbit-flex-direction: column;
    flex-direction: column;
    background: #1E67CB;
}
form{
    cursor: default !important;
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: center;
    justify-content: center;
    -webkit-align-self: center;
    align-self: center;
    -webkit-flex-direction: column;
    flex-direction: column;
    background: #ECF0F1;
    -webkit-box-shadow: 0px 2px 7px rgba(0, 0, 0, 0.40);
    box-shadow: 0px 2px 7px rgba(0, 0, 0, 0.40);
    -webkit-border-radius: 0.3em;
    border-radius: 0.3em;
    padding: 1.3em;
}
form>input{
    width: 22.1em;
    height: 2.5em;
    margin: 0.2em 0;
    font-size: 1em;
    -webkit-font-smoothing: antialiased;
    font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
    text-align: center;
    border: 1px solid #d5dadc;
    -webkit-border-radius: 2px;
    border-radius: 2px;
    color: #7C7C7C;
    outline: none;
}
form>button{
    width: 22.35em;
    height: 2.5em;
    margin: 0.2em 0;
    padding: 0;
    font-size: 1em;
    -webkit-font-smoothing: antialiased;
    font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
    cursor: pointer;
    outline: none;
    border: none;
    color: #fff;
    background: #2ECC71;
    -webkit-border-radius: 2px;
    border-radius: 2px;
}
form>textarea{
    margin: 0.2em 0;
    font-size: 1em;
    -webkit-font-smoothing: antialiased;
    font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
    border: 1px solid #d5dadc;
    -webkit-border-radius: 2px;
    border-radius: 2px;
    color: #7C7C7C;
    outline: none;
    max-width: 22em;
}
mark{
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: center;
    justify-content: center;
    -webkit-align-self: center;
    align-self: center;
    height: 1.5em;
    margin: 1.5em;
    font-size: 1em;
    color: #fff;
    -webkit-font-smoothing: antialiased;
    font-family: HelveticaNeue-Light,"Helevetica Neue",Helvetica,Arial;
    background: none;
}

【讨论】:

  • 表单看起来很棒,现在我只是在测试中。我收到了您的 4 封测试电子邮件,假设您在创建表单时发送了...我尝试发送了几封测试电子邮件,但没有收到...
  • @blackRob4953 检查您的垃圾邮件,您可能有严格的电子邮件过滤器。
  • 遥遥领先哈。那里空无一物。另外,我在收件箱中收到了您的测试电子邮件。我修改了一些东西,但没有什么疯狂的。为 //phtml 创建了一个 php 文件。并将该 php 文件放在表单的操作中。还包括一个 php 文件的 if 语句,如果为真,则指向已发送的消息屏幕,如果为假,则指向未发送的屏幕。目前它总是指向未发送的屏幕。所以它失败了 if 语句
  • @blackRob4953 明白了,肯定是关于电子邮件成功/失败的 if 语句......请稍等一下更新代码。
  • 好的,我刚刚更新了它...请注意,PHP 邮件函数仅针对正确的电子邮件格式返回 TRUE,而不是电子邮件消息实际已送达...php.net/manual/en/function.mail.php(滚动到此函数的返回值)
猜你喜欢
  • 2022-08-13
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
  • 2014-04-15
  • 2018-01-26
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多