#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Processing a form with if empty messages


    I've taken this as an extract of a much larger file.

    If $score1 is empty the no score message appears, the exit() occurs. and the footer div will not appear.
    If $score1 is filled, the success message and the footer div both appear fine.

    If I remove the exit, then both the success and the no score message appears, and the footer div displays fine.

    I would like the no score message to appear when $score1 is empty, aswell as the footer div to appear.

    How should I restructure my code so that is so?

    Any basic examples would be great!


    Code:
    <input type="text"   name="score1"/>
    <input type="text"   name="score2"/>
    
    <?php 
     if(isset($_POST['processForm'])) 
       {
    	   $score1 = $_POST['score']; 
    	   if (empty($score1)) {echo "You haven't entered a score for score2"; exit();}
    	   else if (empty($score2)) {echo "You haven't entered a score for score1"; exit();}
              
    #connection here
    $sql = "INSERT STATEMENT HERE";
    mysql_query($sql);
    			
    echo "success message";  
       
       };
    ?>
    </form>
    <div id="footer">Footer</div>
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    simply put the INSERT stuff and the success message into the "else" part of the "if" statement.

    You should generally avoid using "exit" (except for special cases like redirects), because it interfers with the expected flow of control and makes the code less readable -- kind of like the "goto" in ancient languages. It's better to used structured code with "if" and "else".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,604
    Rep Power
    595
    I don't know how this can work at all based on what I see. First, you set score1 to $_POST['score'] which you do not show as an input. Second you do not seem to set $score2 at all before trying use it. Third your error messages appear to be the opposite of what you are testing. Fourth, please use [ PHP ] tags for your code. See the sticky at the top of this forum.
    PHP Code:
    <input type="text"   name="score1"/>
    <input type="text"   name="score2"/>

    <?php 
     
    if(isset($_POST['processForm'])) 
    {
       
    $score1 $_POST['score1']; 
       
    $score2 $_POST['score2'];

      if (!empty(
    $score1) && !empty($score2)) {
          
    #connection here
          
    $sql "INSERT STATEMENT HERE";
          
    mysql_query($sql);
                
          echo 
    "success message";  
          echo 
    "</form>";
          echo 
    "<div id="footer">Footer</div>";
       }
       else {echo 
    "You haven't entered a score for both score1 and score2</form>";}
    ?>
    Finally, do not use the deprecated MySQL extensions. Switch to PDO.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo