Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
mike316

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.

Share this post


Link to post
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.

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...