Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts

    EASY Form Validation

    PHP Code:
    <?php

    # Alias: Phil Jackson
    # ACT Web Designs
    # http://actwebdesigns.co.uk
    # PHP Form Validation

    # below is a simple form that I have added just incase ;-)

    #<form method="post" action="./root-to-this-file.php">
    #    <p><label for="email">Email: </label><input name="email" type="text" /></p>
    #    <p><label for="email">Phone Number: </label><input name="phone_number" type="text" /></p>
    #    <p><label for="email">Post Code: </label><input name="post_code" type="text" /></p>
    #    <p><input type="submit" name="submit-form" value="send" />
    #</form>

    # alter the details below to suit.
    # to add more data into the equasion simply add another form input; 
    # <p><label for="username">User Name: </label><input name="username" type="text" /></p>
    # and then add the details in the master array. If you do not have nor need a regular
    # expression, then just add '' which will then check to see if it both exists and that
    # it is not empty. I have added the example commented out in the master array.
    # all data passed is cleansed and should not cause any problems.

    # inspired by the good folk of codingforums.com!

    define"SUBMIT_BUTTON_NAME"'submit-form' );
    define"DB_SERVER"'localhost' );
    define"DB_USERNAME"'root' );
    define"DB_PASSWORD"'' );
    define"DB_NAME"'test' );
    define"TABLE_NAME"'test' );
    define"RETURN_ADDRESS"'http://www.actwebdesigns.co.uk' );
    define"MSG""Successfully inserted data into table" );

    $master = array( 
        
    'input_name' => array( 
                              
    'email'
                              
    'phone_number'
                              
    'post_code' 
                              
    // 'username' ( but dont forget to add a comma after post code )
                        
    ), 
        
    'regex' => array( 
                         
    '^[_a-z0-9\-]+(\.[_a-z0-9\-]+)*@[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.[a-z]{2,3})$'
                         
    '^\d{5}\s?\d{6}$'
                         
    '^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$'
                         
    // '' ( leave empty if only want to check if input is set and not empty, dont forget to add the comma above )
                    
    ),
        
    'error' => array( 
                         
    'Incorrect email format'
                         
    'Incorrect phone number format'
                         
    'Incorrect UK post code format' 
                         
    // 'Please enter a username ( dont forget to add the comma above )
                    
    ), 
        
    'db_col' => array( 
                          
    'EMAIL'
                          
    'PHONE_NUMBER'
                          
    'POST_CODE' 
                          
    // 'USERNAME' ( dont forget the comma!! )
                    
    )
    );

    ############################################
    ############# NO NEED TO EDIT BELOW ########
    ############################################

    if( isset( $_POST[SUBMIT_BUTTON_NAME] ) ) {
        
    $CON mysql_connectDB_SERVERDB_USERNAMEDB_PASSWORD ) or die( "Error: on line " __LINE__ " located at " __FILE__ " " .  mysql_error() );
        
    mysql_select_dbDB_NAME$CON );
        function 
    filter$data ) {
            
    $data trimhtmlentitiesmb_convert_encoding$data'HTML-ENTITIES'"UTF-8") ) ); 
            if ( 
    get_magic_quotes_gpc() ) { 
                
    $data stripslashes$data ); 
            } 
            
    $data mysql_real_escape_string$data ); 
            return 
    $data;
        }
        foreach( 
    $_POST as $key => $val ) {
            
    $post[$key] = $val;
            unset( 
    $_POST[$key] );
        }
        foreach( 
    $master['input_name'] as $key => $input_name ) {
            if( ! isset( 
    $post[$input_name] ) || empty( $post[$input_name] ) ) {
                
    $msg $master['error'][$key];
                break;
            }elseif( ! empty ( 
    $master['regex'][$key] ) && ! preg_match"#" $master['regex'][$key] . "#is"$post[$input_name] ) ) {
                
    $msg $master['error'][$key];
                break;    
            }
        }
        if( ! isset( 
    $msg ) ) {
            
    $col_str '';
            
    $val_str '';
            foreach( 
    $master['db_col'] as $key => $col_name ) {
                
    $col_str .= "`" $col_name "`, ";
                
    $val_str .= "'" $post[$master['input_name'][$key]] . "', ";
            }
            
    $insert_query "INSERT INTO `" TABLE_NAME "` ( " substr$col_str0, -) . " ) VALUES ( " substr$val_str0, -) . " )";
            
    mysql_query$insert_query$CON ) or die( "Error: on line " __LINE__ " located at " __FILE__ " " .  mysql_error() );
            
    $msg MSG;
        }
        
    mysql_close$CON );
        
    header"location: " RETURN_ADDRESS "?msg=" urlencode$msg ) );
        exit;
    }

    ?>
    I must admit I havn't had time to test it, it's just I saw jet again another post about form validation and thought this might help folk.
    Was also going to do the jquery function but wasn't sure administration would allow me to post with this code.
    Last edited by Phil Jackson; 04-29-2010 at 05:56 PM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    non dribble version:

    PHP Code:
    <?php

    # http://actwebdesigns.co.uk

    define"SUBMIT_BUTTON_NAME"'submit-form' );
    define"DB_SERVER"'localhost' );
    define"DB_USERNAME"'root' );
    define"DB_PASSWORD"'' );
    define"DB_NAME"'test' );
    define"TABLE_NAME"'test' );
    define"RETURN_ADDRESS"'http://www.actwebdesigns.co.uk' );
    define"MSG""Successfully inserted data into table" );

    $master = array( 
        
    'input_name' => array( 
                              
    'email'
                              
    'phone_number'
                              
    'post_code' 
                        
    ), 
        
    'regex' => array( 
                         
    '^[_a-z0-9\-]+(\.[_a-z0-9\-]+)*@[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.[a-z]{2,3})$'
                         
    '^\d{5}\s?\d{6}$'
                         
    '^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$'
                    
    ),
        
    'error' => array( 
                         
    'Incorrect email format'
                         
    'Incorrect phone number format'
                         
    'Incorrect UK post code format' 
                    
    ), 
        
    'db_col' => array( 
                          
    'EMAIL'
                          
    'PHONE_NUMBER'
                          
    'POST_CODE' 
                    
    )
    );

    if( isset( 
    $_POST[SUBMIT_BUTTON_NAME] ) ) { $CON mysql_connectDB_SERVERDB_USERNAMEDB_PASSWORD ) or die( "Error: on line " __LINE__ " located at " __FILE__ " " .  mysql_error() );
    mysql_select_dbDB_NAME$CON ); function filter$data ) { $data trimhtmlentitiesmb_convert_encoding$data'HTML-ENTITIES'"UTF-8") ) ); 
    if ( 
    get_magic_quotes_gpc() ) { $data stripslashes$data ); } $data mysql_real_escape_string$data ); return $data; }
    foreach( 
    $_POST as $key => $val ) { $post[$key] = $val; unset( $_POST[$key] ); } foreach( $master['input_name'] as $key => $input_name ) {
    if( ! isset( 
    $post[$input_name] ) || empty( $post[$input_name] ) ) { $msg $master['error'][$key]; break; }elseif( ! empty ( $master['regex'][$key] ) && ! preg_match"#" $master['regex'][$key] . "#is"$post[$input_name] ) ) {
    $msg $master['error'][$key]; break; } } if( ! isset( $msg ) ) { $col_str ''$val_str ''; foreach( $master['db_col'] as $key => $col_name ) {
    $col_str .= "`" $col_name "`, "$val_str .= "'" $post[$master['input_name'][$key]] . "', "; } $insert_query "INSERT INTO `" TABLE_NAME "` ( " substr$col_str0, -) . " ) VALUES ( " substr$val_str0, -) . " )";
    mysql_query$insert_query$CON ) or die( "Error: on line " __LINE__ " located at " __FILE__ " " .  mysql_error() ); $msg MSG; } mysql_close$CON );
    header"location: " RETURN_ADDRESS "?msg=" urlencode$msg ) ); exit; }

    ?>
    Last edited by Phil Jackson; 04-29-2010 at 05:56 PM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 


  •  

    Posting Permissions

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