SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Nested IF-THEN-ELSE not working

    Sorry for such a basic question, but why is this nested conditional not working?!

    If $new_cust is FALSE then the code goes to the hightlighted red code below which is clearly wrong.

    Code:
      if (isset($submitted)) {
        // Handle the form.
        if ($new_cust == TRUE) {
          // For New Customers
          if(empty($email)) {
            $errors['email']='Please enter a valid E-mail address.';
          };
        } else {
          // For Existing Customers
          if(empty($email)) {
            $errors['email']='Please enter a valid E-mail address.';
          };
    
          if(empty($password1)) {
            $errors['password1']='Please enter your Password valid E-mail address.';
          };
        }
      }

    Amy

  2. #2
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    589
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how does it validates if it's true or false? is it passed via post? like:

    $new_cus = $_POST['newcus'];

    if this is the case then it should be:

    if ($new_cust == 'TRUE')

    OR if you have a function that returns whether it's true or false then it should be

    if ($new_cust = truefalseFunction());

    w/c will give you :

    ($new_cust = TRUE)

    Goodluck
    Don't Panic

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A good basic debugging tactic is to make liberal use of var_dump()

    For example, var_dump($new_cust);

    My guess is that php is going to tell you it is string FALSE, not boolean false. Most strings convert to boolean true when using you do a loose comparison to a boolean.

    http://www.php.net/manual/en/types.comparisons.php

    Likewise, you can see the boolean result
    var_dump($new_cust == TRUE);


    If you don't want php to perform this automatic type coercion for you, use some other methods to compare. For example, use the strict equality operators === !==


    values which come from php's superglobals($_GET $_POST $_***** etc...) will almost always be strings. There's also the possibility of arrays, as well as type null when the variable doesn't exist.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also
    PHP Code:
    if ($new_cust == TRUE) { 
    is the same as
    PHP Code:
    if ($new_cust) { 

  5. #5
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On a side note...
    Is there any good reason for the duplicate email checks?
    If $new_cust is true, then you check if email is empty... and if $new_cust is not true, then you also check if email is empty...

    Perhaps you could eliminate a little bit of code, by simply always checking whether email is empty or not..?
    zalucius

  6. #6
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    A good basic debugging tactic is to make liberal use of var_dump()

    For example, var_dump($new_cust);

    My guess is that php is going to tell you it is string FALSE, not boolean false. Most strings convert to boolean true when using you do a loose comparison to a boolean.

    http://www.php.net/manual/en/types.comparisons.php

    Likewise, you can see the boolean result
    var_dump($new_cust == TRUE);


    If you don't want php to perform this automatic type coercion for you, use some other methods to compare. For example, use the strict equality operators === !==


    values which come from php's superglobals($_GET $_POST $_***** etc...) will almost always be strings. There's also the possibility of arrays, as well as type null when the variable doesn't exist.

    I'm using NetBeans...

  7. #7
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zalucius View Post
    On a side note...
    Is there any good reason for the duplicate email checks?
    If $new_cust is true, then you check if email is empty... and if $new_cust is not true, then you also check if email is empty...

    Perhaps you could eliminate a little bit of code, by simply always checking whether email is empty or not..?
    Good point.

  8. #8
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stonedeft View Post
    how does it validates if it's true or false? is it passed via post? like:

    $new_cus = $_POST['newcus'];

    if this is the case then it should be:

    if ($new_cust == 'TRUE')

    OR if you have a function that returns whether it's true or false then it should be

    if ($new_cust = truefalseFunction());

    w/c will give you :

    ($new_cust = TRUE)

    Goodluck

    So you are saying I need to put Booleans inside literals?!

    I thought since TRUE and FALSE were constants that you did not quote them?!

  9. #9
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I just woke up and there are all of the disparate responses?!

    I'm not understanding what everyone is saying?!

    Should I post more of my code?

    I debugged this in Netbeans last night and the variable held FALSE yet it took the wrong branch in my IF-THEN-ELSE.

    Here is more of my code...

    My code:
    Code:
    <?php
      //This page processes...
    
      // Address error-handling.
      error_reporting(E_ALL & ~E_NOTICE);
    
    
      // Start output buffering.
      ob_start();
    
      // Initialize a session.
      session_start();
    
      // Define variables.
      $email = $_POST['email'];
      $new_cust = $_POST['new_cust'];
      $password1 = $_POST['password1'];
      $submitted = $_POST['submitted'];
    
      echo 'email = ' . $email . "<br />";
      echo 'new_cust = ' . $new_cust . "<br />";
      echo 'password1 = ' . $password1 . "<br />";
      echo 'submitted = ' . $submitted . "<br />";
    
      // Initialize $errors array
      $errors= array();
    
      if (isset($submitted)) {
        // Handle the form.
        if ($new_cust == TRUE) {
          // For New Customers
          if(empty($email)) {
            $errors['email']='Please enter a valid E-mail address.';
          };
        } else {
          // For Existing Customers
          if(empty($email)) {
            $errors['email']='Please enter a valid E-mail address.';
          };
    
          if(empty($password1)) {
            $errors['password1']='Please enter your Password valid E-mail address.';
          };
        }
      }
    /*
        if(empty($errors)) {
            //validation success
            //send email, save to db ...
            header('Location: thankyou.html');
        }
    */
    
      ob_end_flush();
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
    <head>
      <title>Sign-In/Create Account</title>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
      <!--<link rel="stylesheet" type="text/css" href="tabs.css" />-->
      <style type="text/css">
        /* DEV: Identify Element Edges */
        /*
        * {
            border: 1px dotted #FF0000;
        }
        */
    
    
        html,
        body {
          margin: 0px;
          padding: 0px;
          background-color: #FFFFFF;            /* White */
          font-family: Arial, Helvetica, sans-serif;
          font-size: 1em;   /*0.8em/1.8em*/
          color: #000000;                       /* Black */
        }
    
        #wrapper {
          overflow: hidden;     /* Helps to "contain" floated children. */
          width: 850px;
          margin: 70px Auto;
          padding: 50px;
          border: 1px solid #808080;
        }
    
        #header {
          float: left;
          width: 100%;
          margin: 0em 0em 1em 0em;
          padding: 0em;
          font-size: 1.4em;
          font-weight: bold;
        }
    
        #content {
          clear: both;
        }
    
        #content h2 {
          margin: 0px 0px -2px 0px;
          font-size: 1em;
          color: #FF6600;
        }
    
        p {
          margin: 0 0 1em;
          font-size: 0.8em;
        }
    
        /* PAGE CONTENT STYLES */
        form {
          padding: 0px 0px 0px 150px;     /* padding: 0px 0px 0px 200px */
        }
    
        ol {
          margin: 0px;
          padding: 0px 0px 30px 20px;
          list-style-type: lower-alpha;
        }
    
        .step {
          margin: 14px 0px 0px 0px;
        }
    
        label {
          display: block;
          padding: 0px 0px 5px 0px;
          font-weight: bold;
        }
    
        input {
          /*
          display: block;
          */
          font-size: 1em;
        }
    
        .cust_status label {
          display: inline;
          padding: 0px 0px 5px 0px;
          font-weight: normal;
        }
    
        .cust_status input {
          display: inline;
          font-size: 1em;
        }
    
        .proceed {
          margin: 14px 0px 20px 20px;
        }
    
        a {
          outline: none;
          font-size: 0.8em;
        }
    
        .error {
          color: #FF0000;
          padding: 0px 0px 0px 20px;
        }
    
      </style>
    </head>
    <body>
      <div id="wrapper">
        <!--####################  PAGE HEADER  ####################-->
        <div id="header">
          STEP 2: Sign-In/Create Account
        </div>
    
        <!--####################  PAGE CONTENT  ####################-->
        <div id="content">
          <h2>Registering is quick and easy...</h2>
    
          <!-- LOG-IN FORM -->
    <!--      <form action="104_CreateUser.xhtml" method="post">  -->
          <form action="103_SignIn.php" method="post">
            <ol>
              <!-- a.) E-MAIL ADDRESS -->
              <li class="step">
                <label for="email">Enter your e-mail address:</label>
                <input id="email" type="text" name="email" size="30" maxlength="80" />
                <?php if(isset($errors['email'])) {
                        echo '<span class="error">' . $errors['email'] . "</span>";
                      } ?>
              </li>
    
              <!-- b.) CUSTOMER STATUS -->
              <li class="step">
                <label  for="radio">Choose a customer status:</label>
                <div class="cust_status" id="radio">
                  <div>
                    <input id="new_cust" type="radio" name="new_cust" value="TRUE" checked="checked"/>
                    <label for="new_cust">I'm a new customer</label>
                  </div>
                  <div>
                    <input id="ret_cust" type="radio" name="new_cust" value="FALSE" />
                    <label for="ret_cust">I'm a returning customer</label>
                  </div>
                </div>
              </li>
    
              <!-- c.) PASSWORD -->
              <li class="step">
                <label for="password">Returning customers, what is your password?</label>
                <input id="password" type="password" name="password1" size="20" maxlength="20" />
                <?php if(isset($errors['password1'])) {
                        echo '<span class="error">' . $errors['password1'] . "</span>";
                      } ?>
              </li>
            </ol>
            <div>
            <!-- HIDDEN INPUT -->
            <input type="hidden" name="submitted" value="TRUE" />
    
            <!-- PROCEED BUTTON -->
            <input type="image" src="images/Proceed.jpg"
                   alt="Proceed" class="proceed" border="0" /><br />
    
            <!-- FORGOT PASSWORD LINK -->
            <a href="#" class="proceed">Help, I Forget My Password?!</a>
            </div>
          </form>
        </div>
      </div>
    </body>
    </html>

    Testing:
    If I run the form, select "I am a returning customer" (i.e. New Customer = FALSE), and leave Email and Password blank, I expect to see...

    Please enter a valid E-mail address.

    Please enter your Password.


    However my code is taking the wrong path even though new_cust = FALSE in NetBeans.

    It is taking this path...

    if (isset($submitted)) {
    // Handle the form.
    if ($new_cust == TRUE) {
    // For New Customers
    if(empty($email)) {
    $errors['email']='Please enter a valid E-mail address.';
    };


    Amy

  10. #10
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Apparently...

    Code:
    if ($new_cust == 'TRUE') {
    fixed my problem.


    I know one person said that last night, but everyone's comments were so all over the map that I was overwhelmed what everyone was saying.

    Can someone please explain...

    Why aren't values set in my radio buttons...

    Code:
                  <div>
                    <input id="new_cust" type="radio" name="new_cust" 
    value="TRUE" checked="checked"/>
                    <label for="new_cust">I'm a new customer</label>
                  </div>
                  <div>
                    <input id="ret_cust" type="radio" name="new_cust" 
    value="FALSE" />
                    <label for="ret_cust">I'm a returning customer</label>
                  </div>

    treated as Booleans in my PHP code??



    Amy

  11. #11
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,099
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    value="TRUE" sets TRUE to a string, not a boolean try value="yes" and it's less confusing.

    Just my 2c
    What I lack in acuracy I make up for in misteaks

  12. #12
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lorenw View Post
    value="TRUE" sets TRUE to a string, not a boolean try value="yes" and it's less confusing.

    Just my 2c
    Thanks!!!

    (Where were you last night when I wasted an hour freaking out over this basic issue?!)

    It always amazes me how these petty issues trip you up forever - especially when you're learning a new language?! UGH!!


    Amy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •