www.webdeveloper.com
Results 1 to 2 of 2

Thread: processing a form with if empty messages

  1. #1
    Join Date
    Sep 2011
    Posts
    8

    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
    Join Date
    Jul 2007
    Location
    Wisconsin
    Posts
    468
    PHP Code:
    <input type="text"   name="score1"/>
    <input type="text"   name="score2"/>

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

        
    // VERY IMPORTANT RULE:
        //    a "hard" exit(); in your code is a VERY bad idea, 
        //    unless you explicitly add notifications before you exit.
        //    exit() does NOT break out of a loop, it "shuts down" PHP.
        //    You'll end up with blank white pages, 
        //    and no reference to what or where it happened.

        // create a tmp var that you can toggle
        
    $tmpProcess=TRUE;

        
    // I assume, based on your form that this SHOULD BE 'score1' and not 'score'
        
    $score1 $_POST['score1'];
        
    // I assume that you need this var set as well, based on the form above ??
        
    $score2 $_POST['score2'];

        
    // these variables look backwards... check 1 -> report 2 ?
        
    if (empty($score1)) {
            
    $tmpProcess=FALSE;
            echo 
    "You haven't entered a score for score2";    
        }
        else if (empty(
    $score2)) {
            
    $tmpProcess=FALSE;
            echo 
    "You haven't entered a score for score1";
        }

        
    // by changing the tmp var above, you can avoid running the insert query 
        // if you have blank values.  I'm guessing you need both 
        // $score1 and $score2 to properly insert?
        
    if ($tmpProcess===TRUE) {
            
    #connection here
            
    $sql "INSERT STATEMENT HERE";
            
    mysql_query($sql);

            echo 
    "success message";            
        }    
    // end $tmpProcess        

    }

    ?>
    </form>
    <div id="footer">Footer</div>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles