PHP-表单验证错误

时间:2016-03-07 16:05:26

标签: php html validation

这是我第一次验证,我已经度过了最艰难的时光。我有一个需要验证的注册表,我已经尝试了2个脚本。最好的脚本可以在下面看到:但是每当我尝试回显错误消息以显示在我的文本字段下时,我收到以下错误消息:


注意:未定义的变量: /Applications/MAMP/htdocs/PhpProject2/Reg_1.php 中的c_email 161

注意:未定义的变量:第163行的/Applications/MAMP/htdocs/PhpProject2/Reg_1.php中的c_emailErr

注意:未定义的变量:第169行的/Applications/MAMP/htdocs/PhpProject2/Reg_1.php中的c_pass1Err

C_emailErr和c_pass1Err都已定义。

任何帮助将不胜感激。

HTML

         <section class="container">
        <form id="myform " class="Form" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" accept-charset="utf-8">

            <!--<div id="first">-->
            <input type="email" id="email" name="email" placeholder="Email Address" value="<?= $c_email ?>" required >
            <br>
            <span class="error"><?php echo $c_emailErr; ?></span>
            <br>

            <figure>
                <input class ="login-field" type="password" id="pass1" name="pass1" value="<?= $c_pass1 ?>" placeholder="Password"  maxlength="30" required>
                <br>
                <span class="error"><?php echo $c_pass1Err; ?></span>
                <br>
                <input class ="login-field" type="password" id="pass2" name="pass2" value="" placeholder=" Confirm password" maxlength="30" required><!--<span class="error"><?php //echo $c_pass2Err;              ?></span>-->
                <div id="messages"></div>
            </figure>
            <p class="remember_me">
            </p>
            <input type="submit" name="submit" value="Register" id="submit_button" class="btn btn-default">
            <br>
        </form>

        <?php

        ?>

    </form>
</section>

PHP

 <?php
                if (isset($_POST['submit'])) {


                    $c_email = $_POST['email'];
                    $c_pass1 = $_POST['pass1'];
                    $c_pass2 = $_POST['pass2'];
                    $c_emailErr = $c_pass1Err = $c_pass2Err = "";
                    //Checking the email address
                    if (!filter_var($c_email, FILTER_VALIDATE_EMAIL) === false) {
                        echo ("<b  id='email'> This is a valid email address </b>");
                    } else {
                        echo ("<b id='email'> Email is not a valid email address</b>");
                    }
                    if (strlen($c_pass1) <= '8') {
                        echo "<b>Your Password Must Contain At Least 8  Characters!</br>";
                        //check passwords
                    } elseif ($c_pass1 == $c_pass2) {
                        $q = "INSERT INTO Cus_Register(Cus_Email,Cus_Password,Cus_confirm_password) VALUES (?,?,?)";
                        $stmt = mysqli_prepare($dbc, $q);
                        //new
                        // $stmt = mysqli_prepare($dbc, $insert_c);
                        //debugging
                        //$stmt = mysqli_prepare($dbc, $insert_c)  or die(mysqli_error($dbc));

                        mysqli_stmt_bind_param($stmt, 'sss', $c_email, $c_pass1, $c_pass2);


                        if ($q) {
                            echo "<script> alert('registration sucessful')</script>";
                        }
                    } else {
                        echo "<b>Oops! Your passwords do not </b>";
                    }
                }

                ?>  

1 个答案:

答案 0 :(得分:2)

您正在定义这些变量,但是您要在if块内定义它们。将它们移到if块之外。

<?php
 $c_emailErr = $c_pass1Err = $c_pass2Err = "";

 if (isset($_POST['submit'])) {
     $c_email = $_POST['email'];
     $c_pass1 = $_POST['pass1'];
     $c_pass2 = $_POST['pass2'];