Results 1 to 3 of 3

Thread: Displaying Form Error Messages

Threaded View

  1. #1

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Displaying Form Error Messages

    You have a situation where you want to redirect the user back to the form should it have any errors. Ideally you would like to signify which fields need attention and why.

    A good way of doing this is to use a session to temporarily to store the form information. Here is the logic behind it - it has worked well for me and I use it in my site at the moment.

    First we have a form template:

    form_template.php
    PHP Code:
    <?php
        
    if (isset($_GET['errors'])) {
            
    /* this means our form has been rejected due to errors
               a session should have been created in the form processor
               and we will start it here
             */
             
    session_start();

             
    /* load the form data from the session into a local variable */
             
    $form_data = @$_SESSION['form_data'];

             
    /* now we've collected the form data we can delete the session */
             
    session_destroy();
        }

    /* below is the template we use the @ suppression operator to prevent
       undeclared variable notices from display if there weren't any errors */
    ?>
    <html>
        <head>
            <title>Form Redirection Example</title>
        </head>
        <body>
            <form method="post" action="form_processor.php">
                <p><?php echo(@$form_data['err']) ?></p>
                <table>
                    <tr>
                        <td></td>
                        <td><?php echo(@$form_data['string']['err']) ?></td>
                    </tr>
                    <tr>
                        <td>Enter some text here:</td>
                        <td><input type="text" name="string"
                                   value="<?php echo(@$form_data['string']['value']) ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><?php echo(@$form_data['number']['err']) ?></td>
                    </tr>
                    <tr>
                        <td>Enter a number here here:</td>
                        <td><input type="text" name="number"
                                   value="<?php echo(@$form_data['number']['value']) ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Submit Form" /></td>
                    </tr>
                </table>    
            </form>
        </body>
    </html>
    We also need a processing script:

    form_processor.php
    PHP Code:
    <?php
        $form_data 
    = Array();
        
    $error false;
        
        
    /* check for data */
        
    if (! posted('string''number')) {
            
    $form_data['err'] = 'Invalid data format.';
            
    $error true;
        } else {
            
    // assume magic quotes is off
            
    $string $_POST['string'];
            
    $number $_POST['number'];

            if (
    $string == '' ) { // its been left blank
                
    $form_data['string']['err'] = 'This field is required.';
                
    $error true;
            }

            if (
    $number == '') { // its been left blank
                
    $form_data['number']['err'] = 'This field is required.';
                
    $error true;
            } else if (! 
    is_numeric($number)) { // not a number
                
    $form_data['number']['err'] = 'This field must be a number.';
                
    $error true;
            }
        }
        
        
    /*  check if any errors occured */
        
    if ($error) {    
            if (! isset(
    $form_data['err'])) {
                
    $form_data['string']['value'] = htmlspecialchars($string);
                
    $form_data['number']['value'] = htmlspecialchars($number);
            }
        
            
    // force the session to use the query string
            
    ini_set('session.use_cookies''0');
            
    session_start();
            
            
    /* store the errors and data tempraily in the session */
            
    $_SESSION['form_data'] = $form_data;
        
            
    /* go back to the form */
            
    header('Location: form_template.php?errors=1&' SID);
        } else {
            
    /* we could redirect to the success page here */
            
    echo('success');
        }

        
    /* helper function to test variables were submitted via HTTP POST */
        
    function posted()
        {
            
    $num_args func_num_args();
            
    $args func_get_args();

            for(
    $i 0$i $num_args$i++) {
                if (! isset(
    $_POST[$args[$i]])) {
                    return 
    false;
                }
            }
            
            return 
    true;
        }
    ?>
    You can see a working example here:

    http://adam.codedv.com/examples/form...m_template.php
    Last edited by visualAd; May 19th, 2005 at 04:02 PM.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width