SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form validation: display error messages questions.

    Hi dears

    I have a little question about the best logical way to display error messages when a user forgot to fill in a form field. I am trying to avoid the awful 'echo-blank-page' solution here. I have been trying a few things instinctively, but I think that I need a little boost here from more knowledgeable people than me.

    Here we go (that's my actual code, in progress, and that's as far as I can go by myself):
    PHP Code:
    //if the member has submitted the text to be edited yet.

    $error='';

    if ( isset(
    $_POST['edit']) ) {
        
        
    //just a patanoid check.
        
    if ( $_POST['userID'] != $_SESSION['userID'] ) {
            
    $auth->logout();
        }else{
        
        
    //check that the fields are not empty
        
            
    if ( ($_POST['title']='') or ($_POST['text']!='') ) {
                
    $error .= 'Please make sure that you have filled in all the fields.';
                
    $errorCheck true;
            }else{
                
    $title=$_POST['title'];
                
    $text=$_POST['text'];
                
    $textID=$_POST['textID'];
                
    $userID=$_SESSION['userID'];
        
                
    $sql =
                 
    "UPDATE userstexts 
                 SET
                 textTitle = '
    $title'
                 , textText = '
    $text'
                 , newAdmin = 1
                 WHERE
                 textID = 
    $textID
                 AND userID = 
    $userID";
     
                 
    $db->query($sql);

                
    header("Location:userCP_index.php"); 
                exit;
            }
        }


    //if the member has not submitted the text to be edited yet.
    if ( !isset($_POST['edit']) ) {
        
        
    //**********checks that both textID and userID are set, if not, log out!**********
            
        
    if ( (!isset($_GET['textID'])) or (!isset($_SESSION['userID'])) ) {
            
    $auth->logout();
        }else{
            
    $textID=$_GET['textID'];
            
    $userID=$_SESSION['userID'];
        }

        
    $sql =
        
    "SELECT textID
        , textTitle
        , textText 
        FROM userstexts 
        WHERE textID = 
    $textID 
        AND userID = 
    $userID
        ORDER BY textID DESC"

            
        
    $result=$db->query($sql);
            
        
    //**********the users must have texts to edit**********
        //**********if he tries to access this page without any texts to edit,**********
        //**********he gets logged out.**********
            
        
    if ( $result->size() == ) {
            
    $auth->logout();
        }
            
        
    $row $result->fetch(); 
    and here is the form (in the same page) with the error message to display:
    PHP Code:
                <p><?=$error?></p>
                
                
                
                <form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
        
                <div class="quickReply">
                    <p class="centre" style="text-align:centre;">
                    <input type="text" name="title" id="title" value="<?=$row['textTitle']?>" />
                    </p>
                    <p class="centre" style="text-align:centre;">
                    <textarea rows="10" cols="30" name="text" id="text"><?=$row['textText']?></textarea>
                    </p>
                    <input type="hidden" name="textID" value="<?=$textID?>" />
                    <input type="hidden" name="userID" value="<?=$userID?>" />
                    <p class="centre" style="text-align:centre;">
                    <input type="submit" name="edit" value="edit" />
                </div>
            
                </form>
    thanks in advance for your help

  2. #2
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again

    Ok I have found the solution. It was about the place of my ifs and elses.

    Here is the solution, just in case. Please feel free to critic my code, that's the best way for me to improve my skills:
    PHP Code:

    //if the member has submitted the text to be edited yet.

    $error='';

    if ( isset(
    $_POST['edit']) ) {
        
    //check that the fields are not empty
        
    if ( ($_POST['title']=='') or ($_POST['text']=='') ) {
            
    $error .= 'Please make sure that you have both the title and text fields are not empty';
            
    $errorCheck 1;
        }else{
            
    $title=$_POST['title'];
            
    $text=$_POST['text'];
            
    $textID=$_POST['textID'];
            
    $userID=$_SESSION['userID'];
        
            
    $sql =
             
    "UPDATE userstexts 
             SET
             textTitle = '
    $title'
             , textText = '
    $text'
             , newAdmin = 1
             WHERE
             textID = 
    $textID
             AND userID = 
    $userID";
     
             
    $db->query($sql);

            
    header("Location:userCP_index.php"); 
            exit;
        }
    }

    //if the member has not submitted the text to be edited yet.
    if ( !isset($_POST['edit']) or $errorCheck == ) {
        
        
    //**********checks that both textID and userID are set, if not, log out!**********
            
        
    if ( (!isset($_GET['textID'])) or (!isset($_SESSION['userID'])) ) {
            
    $auth->logout();
        }else{
            
    $textID=$_GET['textID'];
            
    $userID=$_SESSION['userID'];
        }

        
    $sql =
        
    "SELECT textID
        , textTitle
        , textText 
        FROM userstexts 
        WHERE textID = 
    $textID 
        AND userID = 
    $userID
        ORDER BY textID DESC"

            
        
    $result=$db->query($sql);
            
        
    //**********the users must have texts to edit**********
        //**********if he tries to access this page without any texts to edit,**********
        //**********he gets logged out.**********
            
        
    if ( $result->size() == ) {
            
    $auth->logout();
        }
            
        
    $row $result->fetch(); 

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    duuudie,

    Hi again!

    What you've done is learn about "sticky forms" about a day or so after I'd studied up on them (good section in Ullman's book, too).

    The LOGIC I've used has been:

    Code:
    1. Test for 'submit' and VALUE of 'submit' to ensure it came 
        from my form.
    
    1a. Create a blank $error message.
    1b. Test each form value and, if error, ADD to $error (including 
         <p style='color:red;' > ... </p> tags)
    
    2. Test if $error NOT still empty
    
    2a. PREpend a header on the $error message (<h3 style ...)
    
    ELSE
    
    2b. use header('Location: Processing.php'); to send to the 
         processing page (if different)
    2c. exit;
    
    3.  Write webpage header info down to text that you want to 
         replace with the $error message.
    
    4.  Test 'submit' again ... same as above.
    
    4a.  echo $error message
    
    ELSE
    
    4b.  echo original introductory (PRIOR to form) text
    
    5.  Write form
    
    5a.  Be sure to include 
          value="<?= isset($_POST['formelementname'])
          ? echo $_POST['formelementname]' 
          : echo 'default value'; ?>" 
          to retain the values submitted in any form submission 
          which failed.
    Okay, you'd said you'd found your error (placement of IF and ELSE constructs) but I hope that this helped anyway.

    Regards,

    DK
    Last edited by dklynn; May 31, 2004 at 16:32.
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn
    duuudie,
    Okay, you'd said you'd found your error (placement of IF and ELSE constructs) but I hope that this helped anyway.
    indeed

    thanks a lot

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    duuudie,

    Quote Originally Posted by duuudie
    indeed

    thanks a lot
    You're very welcome.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  6. #6
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also if you wanted the errors to appear beside the field it's for, you could put the form in a function and have the errors passed as parameters to it and echo the errors wherever you like.


Bookmarks

Posting Permissions

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