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 1 of 1
  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 + EMAIL

    oky this one is for folk who want form validation AND be able to send an email:

    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"'./test2.php' );
    define"MSG""Successfully inserted data into table" );

    define"EMAIL_FORM"true ); # Define as true if you want to send emails
    define"SEND_TO""luke@actwebdesigns.co.uk" );
    define"EMAIL_TEMP_DIR""./email.txt" );
    define"SUBJECT""Message from example" );
    define"FROM""do-not-reply@example.com" );

    if( 
    EMAIL_FORM ) {
        if( ! 
    file_existsEMAIL_TEMP_DIR ) )
            die( 
    "email template not found at : " EMAIL_TEMP_DIR );    
        else
            
    $template file_get_contentsEMAIL_TEMP_DIR );
    }

    $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;
        }
        
        if( 
    EMAIL_FORM ) {
            foreach( 
    $master['input_name'] as $key => $input_name ) {
                
    $template preg_replace"#<%\s*" $input_name "\s*%>#is"$post[$input_name], $template );    
            }
                                        
            
    $headers "From: " FROM "\nContent-Type: text/html; charset=iso-8859-1";
            
    mailSEND_TOSUBJECT$template$headers );
        }
        
        
    mysql_close$CON );
        
    header"location: " RETURN_ADDRESS "?msg=" urlencode$msg ) );
        exit;
    }

    ?>

    Firstly you need to create an email template such as email.txt:

    Code:
    <p>Hello <% email %>, thanks for getting intouch!</p>
    <p>I hope it is ok to contact you on <% phone_number %></p>
    The <% email %> is the name of the input boxes being sent to the form.

    Secodly, edit the following:

    PHP Code:
    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"'./test.php' );
    define"MSG""Successfully inserted data into table" );

    define"EMAIL_FORM"true ); # Define as true if you want to send emails
    define"SEND_TO""example@example.com" );
    define"EMAIL_TEMP_DIR""./email.txt" );
    define"SUBJECT""Message from example" );
    define"FROM""do-not-reply@example.com" ); 
    Thirdly Edit the following:

    PHP Code:
    $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!! )
                    
    )
    ); 
    Add different input names depending on what you are sending and also add regular expressions or '' if you do not have one, add an error for the field if it fails validation and the finaly enter the table column name.

    to change colour of output of message edit:

    PHP Code:
    define"MSG""<p class=\"green\">Successfully inserted data into table</p>" ); 
    PHP Code:
    'error' => array( 
                         
    '<p class="red">Incorrect email format</p>'
                         
    '<p class="red">Incorrect phone number format</p>'
                         
    '<p class="red">Incorrect UK post code format</p>' 
                         
    // 'Please enter a username ( dont forget to add the comma above ) 
    ( you obviously need dome css to style on the return page )

    Code:
    p.green { color:green; }
    p.red { color:red; }
    AND last but not least on the return page have:

    PHP Code:
    if( isset( $_GET['msg'] ) ) {
        echo 
    $_GET['msg'];


    example of email sent:

    Code:
    Hello phil@actwebdesigns.co.uk, thanks for getting intouch!
    I hope it is ok to contact you on 01623 123456
    Last edited by Phil Jackson; 04-29-2010 at 05:57 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
  •