blackRob4953
blackRob4953

Reputation: 1675

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:

<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 = '[email protected]';
$subject = "New Question For Se7en Service!";
$txt = $content;
$headers = array("From: [email protected]", "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>';
    }
}
?>

Upvotes: 1

Views: 151

Answers (1)

Jordan Davis
Jordan Davis

Reputation: 1520

This should do the trick... let me know if I'm missing anything? or you need help understanding it?

Preview:

enter image description here

//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 = '[email protected]';
    $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;
}

Upvotes: 1

Related Questions