How do I make PHP print out N/A if a form space is left empty?

Discussion in 'PHP' started by Ipodman79, Dec 16, 2013.

  1. #1
    I've created a PHP form that collects information from the user using the $_GET variable in PHP. This code then outputs what the user filled in on another page. However I want it to print 'N/A' if the user leaves a space empty.

    I've tried using,

    if ($firstname==' ' && $secondname==' ' && $email==' ' && $phone==' ' && $age==' ' && $position==' ' && $company==' '){
    echo 'N/A';
    }

    What's wrong with this? Why wont it print 'N/A'. I've created the variables $firstname = $_GET['name'];

    Thanks
     
    Solved! View solution.
    Ipodman79, Dec 16, 2013 IP
  2. #2
    ' ' - that has a space. That's not empty.
    <?php
        if (empty($firstname) && empty($secondname) && empty($email) && empty($phone) && empty($age) && empty($position) && empty($company))
        {
            echo 'N/A';
        }
    ?>
    PHP:
     
    CoreyPeerFly, Dec 16, 2013 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Using $_GET-variables, you should always check to see if they're set - proper syntax for checking would be something like this:
    $firstname = (isset($_GET['firstname'])) ? $_GET['firstname'] : 'N/A';
    Then you just use the $firstname-var whereever you need to display either content.
     
    PoPSiCLe, Dec 16, 2013 IP
  4. Ovidiu20

    Ovidiu20 Active Member

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #4
    Hello,
    If you want to check if one of the variables has not been set you should use || istead of && on the syntax:

    <?php
      if (empty($firstname) || empty($secondname) || empty($email) || empty($phone) || empty($age) || empty($position) || empty($company))
      {
          echo 'N/A';
    }
    ?>
    PHP:
     
    Ovidiu20, Dec 16, 2013 IP
  5. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #5
    Ok, thanks for the reply. Sorry if I sound a bit stupid I'm only 14 years old and only just learning PHP. Thanks
     
    Ipodman79, Dec 17, 2013 IP
  6. RiptideSolutions

    RiptideSolutions Active Member

    Messages:
    142
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    65
    #6
    If you want to learn PHP, a website that will teach you at least the basics and more is CodeAcademy.com .. check it out. You might find it worthy of your time.
     
    RiptideSolutions, Dec 17, 2013 IP