php联系表单验证,在没有错误和成功的情况下停止显示表单

php联系表单验证,在没有错误和成功的情况下停止显示表单

问题描述:

Here's my code:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if (empty($_POST["yourname"])) {
        $yournameErr = "Name is required";
    } else {
        $yourname = test_input($_POST["yourname"]);
    }

    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address syntax is valid
        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
            $emailErr = "Invalid email format";
        }
    }

    if (empty($_POST["message"])) {
        $messageErr = "Message is required";
    } else {
        $message = test_input($_POST["message"]);
    }
}

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

I have got to the point where it doesn't show errors but I probably didn't explain myself too clearly. After the point at which it doesn't show error messages anymore, I would like the form to no longer appear and then I can put something down like "Successful." However I can't seem to achieve this.

my form is :

    <form action="contact.php" name="Form1" id="Form1" method="post">
<div>
<label>Your Name:</label>
<br />
<input type="text" name="yourname" id="yourname" placeholder="Full Name" 
    style="border:1;  border-color:#000000; " />
<span class="error">* <?php echo $yournameErr;?></span>
</div>
    <br />
<br />
<div>
<label> Email :</label> <br />
<input name="email" type="text" id="email" size="20" placeholder="Email" 
    style="border:1;  border-color:#000000; " />
<span class="error">* <?php echo $emailErr;?></span>
</div>
<br />
<br />
<div>
<label> Subject : </label><br />
<input name="subject" type="text" id="subject" size="20" placeholder="Subject" 
    style="border:1;  border-color:#000000; "  />
</div>
<br />
<br />
<div>
<label> Message :<br /> </label>
<textarea rows="5" cols="40" name="message" type="text" id="message" 
    placeholder="The message you want to send to us." style="border:1;  border-  
    color:#000000 " >
</textarea>
<span class="error">* <?php echo $messageErr;?></span>
</div> 
<br />
<br />
<div>
<input type="submit" name="button" id="button" style="border:1; border-
    color:#999999; " value="SEND"/>
</div>
</form>

What if you put your errors in an array and put a condition that check that array size and if it is 0 (no errors) echo the success message and don't show the form else join the errors and print it out.

Maybe like this:

contact.php

<?php
$error = array();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["yourname"])) {
        $error['name'] = "Name is required";
    } else {
        $yourname = test_input($_POST["yourname"]);
    }

    if (empty($_POST["email"])) {
        $error['email'] = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address syntax is valid
        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
            $error['email'] = "Invalid email format";
        }
    }

    if (empty($_POST["message"])) {
        $error['message'] = "Message is required";
    } else {
        $message = test_input($_POST["message"]);
    }

    if (!count($error)) {
         $noError = true;
    }
}

$successMessage = isset($noError) ? 'Successful.' : '';

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

function getErrorMessage($type, $error)
{
    return isset($error[$type]) ? $error[$type] : '';
}

if ($successMessage) {
    echo $successMessage;
} else {
?>
<form action="contact.php" name="Form1" id="Form1" method="post">
    <div>
        <label>Your Name:</label>
        <br/>
        <input type="text" name="yourname" id="yourname" placeholder="Full Name"
               style="border:1px;  border-color:#000000; "/>
        <span class="error">* <?php echo getErrorMessage('name', $error); ?></span>
    </div>
    <br/>
    <br/>

    <div>
        <label> Email :</label> <br/>
        <input name="email" type="text" id="email" size="20" placeholder="Email"
               style="border:1px;  border-color:#000000; "/>
        <span class="error">* <?php echo getErrorMessage('email', $error); ?></span>
    </div>
    <br/>
    <br/>

    <div>
        <label> Subject : </label><br/>
        <input name="subject" type="text" id="subject" size="20" placeholder="Subject"
               style="border:1px;  border-color:#000000; "/>
    </div>
    <br/>
    <br/>

    <div>
        <label> Message :<br/> </label>
        <textarea rows="5" cols="40" name="message" type="text" id="message"
                  placeholder="The message you want to send to us." style="border:1px;  border-  
    color:#000000 "></textarea>
        <span class="error">* <?php echo getErrorMessage('message', $error); ?></span>
    </div>
    <br/>
    <br/>

    <div>
        <input type="submit" name="button" id="button" style="border:1px; border-
    color:#999999; " value="SEND"/>
    </div>
</form>
<?php } ?>

According to your code, I'm going to assume that your contact.php is posting to itself. In other words, your PHP code is located above your HTML --as a result of your question that the contact form no longer renders after a request. That is, once the server renders the page, the form tag will not display because there are no errors in the submission or the super global $_POST has been set.

I've slightly altered your code for readability. I included an array that will hold all your error messages throughout your form validation. If there are no errors, then we can simulate a successful submission and thus reflect this result on response. Your form will only display if there are errors OR the post has not been submitted.

Inside your form, you need an input tag of submit. Furthermore, only if errors are indeed set, that we want to display an error specific message. That is why a condition is set for your span tags. Lastly, I included the same condition in your values -an additional feature for remembering what you have entered.

If the form is successfully submitted, then don't render the form tag and, instead, display a message of success along with all the POST data!

<?php

if (isset($_POST['submit'])) {

    $error_message = array();

    if (empty($_POST["yourname"])) {
        $error_message['yournameErr'] = "Name is required";
    } else {
        $yourname = test_input($_POST["yourname"]);
    }

    if (empty($_POST["email"])) {
         $error_message['emailErr'] = "Email is required";
    } elseif (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $_POST["email"])) {
        $error_message['emailErr'] = "Invalid email format";
    } else {
        $email = test_input($_POST["email"]);
    }

    if (empty($_POST["message"])) {
        $error_message['messageErr'] = "Message is required";
    } else {
        $message = test_input($_POST["message"]);
    }

    if(empty($error_message)) {
        // process data from post
        $successful = true;
    }
}

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

?>
<?php if(!empty($error_message) || !isset($_POST['submit'])) : ?>
     <form action="contact.php" name="Form1" id="Form1" method="post">
    <div>
    <label>Your Name:</label>
    <br />
    <input type="text" name="yourname" id="yourname" placeholder="Full Name" 
        style="border:1;  border-color:#000000; " value="<?php if(isset($yourname)) {echo $yourname; }?>" />
    <span class="error">* <?php if(isset($error_message['yournameErr'])){echo $error_message['yournameErr']; }?></span>
    </div>
        <br />
    <br />
    <div>
    <label> Email :</label> <br />
    <input name="email" type="text" id="email" size="20" placeholder="Email" 
        style="border:1;  border-color:#000000; " value="<?php if(isset($email)) { echo $email;}?>" />
    <span class="error">* <?php if(isset($error_message['emailErr'])) {echo $error_message['emailErr'];}?></span>
    </div>
    <br />
    <br />
    <div>
    <label> Subject : </label><br />
    <input name="subject" type="text" id="subject" size="20" placeholder="Subject" 
        style="border:1;  border-color:#000000; "  />
    </div>
    <br />
    <br />
    <div>
    <label> Message :<br /> </label>
    <textarea rows="5" cols="40" name="message" type="text" id="message" placeholder="The message you want to send to us." style="border:1; border-color:#000000"></textarea>
    <span class="error">* <?php if(isset($error_message['messageErr'])) {echo $error_message['messageErr']; }?></span>
    <br>
    <input type="submit" name="submit" value="Submit">
    </div> 
<?php endif; ?>
<?php if(isset($successful)) : ?>
    <p>Successful</p>
    <p>Your name: <?=$yourname;?></p>
    <p>Your email: <?=$email;?></p>
    <p>Your subject: <?=$_POST['subject']?></p>
    <p>Your message: <?=$message;?></p>
    <a href="contact.php">Back to form</a>
<?php endif; ?>