PDA

View Full Version : EASY Form Validation + EMAIL



Phil Jackson
04-28-2010, 11:32 AM
oky this one is for folk who want form validation AND be able to send an email:



<?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_exists( EMAIL_TEMP_DIR ) )
die( "email template not found at : " . EMAIL_TEMP_DIR );
else
$template = file_get_contents( EMAIL_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_connect( DB_SERVER, DB_USERNAME, DB_PASSWORD ) or die( "Error: on line " . __LINE__ . " located at " . __FILE__ . " " . mysql_error() );
mysql_select_db( DB_NAME, $CON );
function filter( $data ) {
$data = trim( htmlentities( mb_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_str, 0, -2 ) . " ) VALUES ( " . substr( $val_str, 0, -2 ) . " )";
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";
mail( SEND_TO, SUBJECT, $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:



<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:



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:



$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:


define( "MSG", "<p class=\"green\">Successfully inserted data into table</p>" );


'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 )


p.green { color:green; }
p.red { color:red; }

AND last but not least on the return page have:



if( isset( $_GET['msg'] ) ) {
echo $_GET['msg'];
}



example of email sent:


Hello phil@actwebdesigns.co.uk, thanks for getting intouch!
I hope it is ok to contact you on 01623 123456