Jump to content
Larry Ullman's Book Forums

Working on chapter 8 pursue nested password conditional page 242


Recommended Posts

The form works if I use the if(!empty) conditional that you see in this form. If the textbox is left empty or the passwords do not match the correct message appear.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Password</title>
</head>
<body>
    <?php
        // Turning on error report 

        ini_set('display_errors', 1);
        error_reporting(E_ALL);

        // Having the form processed on the same page

        if($_SERVER['REQUEST_METHOD'] == 'POST') { 
            $problem = false;
            
            if(!empty($_POST['password'])) {
                if($_POST['password']  != $_POST['confirm_password']) {
                    $problem = true;
                    print '<p>The two emails do not match</p>';
                } // second conditioanal
            } else {
                $problem = true;
                print '<p>The password is empty</p>';
            } // first conditional
            

            
        }
    ?>

    <!-- Standard form with two inputs for the password -->

    <form action="password.php" method="post">
        <p>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password">
        </p>

        <p>
            <label for="confirm_password">Confirm Password:</label>
            <input type="password" id="confirm_password" name="confirm_password">
        </p>

        <p>
            <input type="submit" name="submit" value"Submit!!">
        </p>
    </form>
</body>
</html>

Now if I use this nested conditional statement below instead of the if(!empty), the statements and the form do not work.

<?php
if(empty($_POST['password'])) {
                if($_POST['password'] != $_POST['confirm_password']) {
                    $problem = true;
                    print '<p>The two emails do not match</p>';
                }
            } else {
                $problem = true;
                print '<p>Please enter your email</p>';
            }
?>

I'm lost on why the if(!empty) works, but the if(empty) doesn't work? My last question is, I can use the if(!empty), that would be the correct way to make sure that no field is empty and the two fields match, correct? It just seems "unnatural" and empty would be the correct one to use. I got the idea of using the (!empty) from page 216 from the book. Nice work on the website Larry, I'm digging the new colors, fonts, and general layout. The blue really compliments the white background, and general layout of the site.

Link to comment
Share on other sites

I'm sorry, this is the wrong place to post this question. The question was supposed to be posted in PHP for the Web 5th edition. I was looking up something for MySQL at the time and posted a question in the wrong book forum. Again, I'm sorry for posting this here and maybe Larry can move it to the appropriate place.

Link to comment
Share on other sites

You can swap !empty() for empty() if you're more comfortable with that logic, but you'd have to rework all the other logic, too. For example, your code checks if the password matches the confirmed password if the password is empty. It also sets $problem equal to true if the password is not empty. Both of these are backwards. If you want to use empty(), rework the following subsections, too. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...