Do not insert FORM is user field is empty.

Discussion in 'PHP' started by tomm098, Aug 25, 2009.

  1. #1
    Hey everyone. I have a form, but I want it so that only logged in users can enter information with it. This is the code for the user field.

    <input name="User" type="hidden" id="User" value="<?php echo $_SESSION['MM_Username']; ?>" />

    How can i make it, so that if the user field is empty, ie. the user is not logged in, the form will not get inserted?

    Thanks
     
    tomm098, Aug 25, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php if (!empty($_SESSION['MM_Username'])): ?>
    //form goes here
    <?php endif; ?>
    PHP:
     
    premiumscripts, Aug 26, 2009 IP
  3. tomm098

    tomm098 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey it worked. What if I wanted it to say 'you must be logged in to vote' when they're not logged in?

    like some kind of 'else' statement?
     
    tomm098, Aug 26, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes ofcourse.. You should learn the basics, if you don't know how to do an else statement you really shouldn't be making any adjustments yourself..

    
    <?php if (..): ?>
    
    <?php else: ?>
    
    <?php endif; ?>
    PHP:
     
    premiumscripts, Aug 26, 2009 IP
  5. tomm098

    tomm098 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can you show me how to write the else statement

    I did this but it didnt work..

    <?php else: echo 'You Must Be Logged In' ?>

    Thanks
     
    tomm098, Aug 26, 2009 IP
  6. p4n4d0l

    p4n4d0l Peon

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try to use this:

    
    <?
    if (!empty($_SESSION['MM_Username'])) {
    
    // continue the script
    
    } else {
    
    // stop the script
    echo "You Must Be Logged In";
    
    }
    ?>
    
    PHP:
     
    p4n4d0l, Aug 26, 2009 IP
  7. tomm098

    tomm098 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Nah sorry that didnt work. I have some condition where it must be 2 seperate PHP sections.

    It has to be

    <?php if (!empty($_SESSION['MM_Username'])): ?> AT THE START

    then; <?php else: ?> AT THE END

    but I need to know how to write this else statement, to simply say, 'login to vote'
     
    tomm098, Aug 26, 2009 IP
  8. p4n4d0l

    p4n4d0l Peon

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Try again this script:

    
    <?
    if (!empty($_SESSION['MM_Username'])) {
    ?>
    // continue anything
    
    <?
    } else {
    ?>
    
    // stop the script
    <b>You Must Be Logged In !</b>
    <p>
    login to vote 
    
    <?
    // If you want it stop here, delete command at die() bellow:
    #die();
    }
    ?>
    
    PHP:
     
    p4n4d0l, Aug 26, 2009 IP