如果在php中提交表单后没有空字段,如何显示成功消息

时间:2018-03-13 19:56:59

标签: php

我已经制作了一个html表单并使用php验证它,或者更确切地说是验证的一部分(只检查rquired字段)     这是html代码:

<div class="container">
    <div class="row">
        <div class="col-md-6">
            <h3>Registration Form</h3><br>
            <form action="#" method="post">
                <div class="form-group">
                    <label for="name">Name : * </label><span class="error"><?php echo $nameErr; ?></span>
                    <input id="name" type="text" name="name" class="form-control" placeholder="Type your name here">

                </div>
                <div class="form-group">
                    <label for="username">Username : * </label><span class="error"><?php echo $usernameErr; ?></span>
                    <input id="username" type="text" name="username" class="form-control" placeholder="Type your username here">

                </div>
                <div class="form-group">
                    <label for="password">Password : * </label><span class="error"><?php echo $passwordErr; ?></span>
                    <input id="password" type="password" name="password" class="form-control" placeholder="Type password here">

                </div>
                <div class="form-group">
                    <label for="rpassword">Retype Password : * </label><span class="error"><?php echo $rpasswordErr; ?></span>
                    <input id="rpassword" type="password" name="rpassword" class="form-control" placeholder="Retype password here">

                </div>
                <div class="form-group">
                    <label for="email">Email : * </label><span class="error"><?php echo $emailErr; ?></span>
                    <input id="email" type="email" name="email" class="form-control" placeholder="Type your email here">

                </div>
                <div class="form-group">
                    <label for="phone">Phone No : * </label><span class="error"><?php echo $phoneErr; ?></span>
                    <input id="phone" type="text" name="phone" class="form-control" placeholder="Type your phone here">

                </div>
                <div class="form-group">
                    <label for="dob">Date of Birth : * </label><span class="error"><?php echo $dobErr; ?></span>
                    <input id="dob" type="date" name="dob" class="form-control">

                </div>
                <div class="form-group">
                    <label>Select A Country : * </label><span class="error"><?php echo $countryErr; ?></span>
                    <input id="country" type="text" name="country" class="form-control not-me">

                </div>
                <div class="form-group">
                    <label for="gender">Please Select Your Gender : *</label><span class="error"><?php echo $genderErr; ?></span>
                    <div class="form-control gender">
                        <input type="radio" id="male" name="gender">
                        <label for="male" class="exclude">Male</label>
                        <input type="radio" id="female" name="gender">
                        <label for="female" class="exclude">Female</label>
                    </div>
                </div>
                <input type="submit" name="submit" class="custom-btn" value="Submit" name="reg-form">
            </form>
        </div>
    </div>
</div>

这是用于检查任何字段是否为空的php代码:

<?php
    $nameErr = $usernameErr = $passwordErr = $rpasswordErr = $emailErr = $phoneErr = $dobErr = $countryErr = $genderErr = "";
    $name = $username = $password = $rpassword = $email = $phone = $dob = $country = $gender = "";

    if(isset($_POST["submit"])) {
        if (empty($_POST["name"])) {
           $nameErr = " This field is required";
        }
        else {
           $name = $_POST["name"];
        }
        if (empty($_POST["username"])) {
           $usernameErr = " You must enter a username";
        }
        else {
           $username = $_POST["username"];
        }
        if (empty($_POST["password"])) {
           $passwordErr = " You must type a strong password";
        }
        else {
           $password = $_POST["password"];
        }
        if (empty($_POST["rpassword"])) {
           $rpasswordErr = " Your password didn't match";
        }
        else {
           $rpassword = $_POST["rpassword"];
        }
        if (empty($_POST["email"])) {
           $emailErr = " This is not a valid email address";
        }
        else {
           $email = $_POST["email"];
        }
        if (empty($_POST["phone"])) {
           $phoneErr = " Please enter your contact number";
        }
        else {
           $phone = $_POST["phone"];
        }
        if (empty($_POST["dob"])) {
           $dobErr = " You must fill your Date of Birth";
        }
        else {
           $dob = $_POST["dob"];
        }
        if (empty($_POST["country"])) {
           $countryErr = " You didn't select a country";
        }
        else {
           $country = $_POST["country"];
        }
        if (empty($_POST["gender"])) {
           $genderErr = " You can not leave this field empty";
        }
        else {
           $gender = $_POST["gender"];
        }
    }
?>

现在我只想在提交表单后显示成功消息,如果没有空字段 N.B:这是我的第一个php工作。 提前谢谢!

2 个答案:

答案 0 :(得分:0)

最简单的解决方案是使用简单的标志变量,使用您的代码进行概念验证:

<?php
    $isFullyFilled = true; //flag 

    $nameErr = $usernameErr = $passwordErr = $rpasswordErr = $emailErr = $phoneErr = $dobErr = $countryErr = $genderErr = "";
    $name = $username = $password = $rpassword = $email = $phone = $dob = $country = $gender = "";

    if(isset($_POST["submit"])) {
        if (empty($_POST["name"])) {
           $nameErr = " This field is required";

           $isFullyFilled = false; //this is line which changes flag
        }
        else {
           $name = $_POST["name"];
        }
    .... //many other ifs modified like that
    if($isFullyFilled){
         showMessage("Form is fully filled");
    }

答案 1 :(得分:0)

将以下行添加到php代码的底部:

echo '<script type="text/javascript">';
echo 'alert("the form has been completed")';
echo '</script>';