0

This is my first time validating, I am having the hardest time have spent endless hours on this already. I have a registration form that needs to be validated, i have tried 2 scripts for this. The script that works best can be seen below: however every time I try to echo the error message to display under my text field i receive the following error messages:


Notice: Undefined variable: c_email in /Applications/MAMP/htdocs/PhpProject2/Reg_1.php on line 161

Notice: Undefined variable: c_emailErr in /Applications/MAMP/htdocs/PhpProject2/Reg_1.php on line 163

Notice: Undefined variable: c_pass1Err in /Applications/MAMP/htdocs/PhpProject2/Reg_1.php on line 169

C_emailErr and c_pass1Err are both defined.

any help would be appreciated.

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>";
                    }
                }

                ?>  
2

1 Answer 1

2

You are defining those variables, but you are defining them inside of an if block.. Move them outside of the if block.

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

 if (isset($_POST['submit'])) {
     $c_email = $_POST['email'];
     $c_pass1 = $_POST['pass1'];
     $c_pass2 = $_POST['pass2'];
Sign up to request clarification or add additional context in comments.

2 Comments

OMG! was that it. You are alive saver! thank you so much
No problem! The old second pair of eyes thing :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.