32 users online (1 members and 31 guests)  


  Results 1 to 7 of 7

Related

  1. #1
    thanh_911's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2007
    Posts
    3

    if - else causes the screen to be blank

    HI,
    I'm having a hard tiem trying to figure out what is going on. I made two pages.
    -
    page 1

    <form name="form1" method="post" action="complete.php">
    Name:
    <input name="Name" type="text" size="40">
    Company:
    <input name="Company" type="text" size="40">
    <input type="submit" name="Submit" value="Send">
    </form>


    page 2

    if(!$_POST['Name'] || !$_POST['Company'])
    print "You did not enter your name.\n";

    else
    print "Thank you";


    This is not my entire code, but that's all is needed to make functional. Everytime I use a global variable from another page, the second page becomes blank. But if I use local variables on one page like


    $name="Bob";

    if ($name=="Bob")
    print "Hi Bob.";
    else
    print "who are you?";


    If i do things like this, it's fine. But if I use if-else on the first example then it's a blank page, but if i take the if-else out and make something like:

    print "Thank you $_POST['Name']. information has been sent to $_POST['Company']."

    This works fine. But as soon as I put in the if statement, everthing goes blank

    I need help getting past this hump before even doing anything bigger.

  2. #2
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: if - else causes the screen to be blank

    Have you tried adding the braces to contain the loops?
    Code:
    <form name="form1" method="post" action="complete.php">
    Name:
    <input name="Name" type="text" size="40">
    Company:
    <input name="Company" type="text" size="40">
    <input type="submit" name="Submit" value="Send">
    </form>
    Page 2
    Code:
    if(!$_POST['Name'] || !$_POST['Company']) {
    print "You did not enter your name.\n";
    } else {
    print "Thank you";
    }
    Hang on, though - I think it may be the comparison. null values are not false, therefore try:
    Code:
    if(!isset($_POST['Name']) || !isset($_POST['Company'])) {
    print "You did not enter your name.\n";
    } else {
    print "Thank you";
    }

  3. #3
    thanh_911's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2007
    Posts
    3

    Re: if - else causes the screen to be blank

    It still doesn't work. I'm not sure why it's doing thing. Nothing is displaying. Even the stuff NOT in php tags. I had some like
    <body> Processing......
    <?php
    a ton of stuff here....
    ?>
    </body>
    The word "processing" doesn't even display...and it's not part of the php code.

  4. #4
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: if - else causes the screen to be blank

    Better post the whole page, then - and please enlose it in [ code] tags.

  5. #5
    thanh_911's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2007
    Posts
    3

    Re: if - else causes the screen to be blank

    It's working now. Thanks a lot. It appears that if i use $_POST['name'] in the if/else statements the page is blank. But if I assign it to something first like $name = $_POST['name'] then use it in the if/else statements, then it works fine.

  6. #6
    fongpn's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2007
    Posts
    4

    Re: if - else causes the screen to be blank

    That might be a small mistake. You must define all string before using them and assign them to a variable. Therefore what you did lastly you can get it working. Your PHP script must be defined before use..Bare this in your mind. Will help you to save a lots of time in your future.

  7. #7
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: if - else causes the screen to be blank

    Quote Originally Posted by thanh_911 View Post
    It's working now. Thanks a lot. It appears that if i use $_POST['name'] in the if/else statements the page is blank. But if I assign it to something first like $name = $_POST['name'] then use it in the if/else statements, then it works fine.
    That will be due to the fact that $_POST is an array, and $name is a variable.
    PHP Code:
    if (!$_POST['Name']) { ...      //This will not validate if the array is not set.
    if (!isset($_POST['Name']) { ...//This will return True if set, False if not
    $name $_POST['Name'];         //The unassisgned array value is converted to null in this assignment.
    if (!$name) { ...               //This will return True if $_POST['Name'] was set, False if not 
    Last edited by DeadMeatGF; 02-23-2007 at 06:33 AM.



Tags for this Thread