简体   繁体   English

PHP电子邮件表单验证不正确

[英]PHP Email form validating incorrectly

I have a comment/questions form on my page. 我在页面上有一个评论/问题表格。 Before I implemented the 'submit' code, it would check for validation efforts. 在实施“提交”代码之前,它将检查验证工作。 Now every time I click submit, it proceeds to my 'Message Sent' page, even when I know it won't page the validation. 现在,每当我单击提交时,即使我知道它不会分页验证,它也会进入我的“已发送邮件”页面。

I've checked some related answers, but couldn't get the answers to this one... 我检查了一些相关的答案,但无法获得关于这个答案的答案...

*Note: Removed some values to get to the main point *注意:删除了一些值以达到重点

Main PHP: 主要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>

Error Handling Form 错误处理表格

<?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 (submit-action) Form: 评论处理程序(提交操作)表格:

<?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>';
    }
}
?>

This should do the trick... let me know if I'm missing anything? 这应该可以解决问题...让我知道我是否缺少任何东西? or you need help understanding it? 或者您需要帮助以了解它?

Preview: 预习:

在此处输入图片说明

//PHTML // 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 // 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM