Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Peter Ehrlich - form submission handling
- //Last updated 12-17-09
- // error_reporting(E_ALL); //this is for bugtesting purposes
- // parameters for sending the email
- $subject = "Your Email Subject";
- // parameters for database connection
- $dbHost = "localhost"; //or whatever else, if you have an external db
- $dbName = "some database";
- $dbPass = 'some passwd';
- //initialize things.
- $validated=true;
- $vals = Array();
- //echo "Working..."; //uncomment this to makes sure things are happening
- //these help with DRY form validation. A valid field here exists with some length > 0.
- function process($fieldName){
- global $vals, $validated; //don't use local copies.
- if ((isset($_POST[$fieldName])) && (strlen(trim($_POST[$fieldName])) > 0)) {
- $vals[$fieldName] = stripslashes(strip_tags($_POST[$fieldName])); //strip code injection
- } else {$validated=false; echo "VAL ERROR:".$vals[$fieldName]; $vals[$fieldName] = 'null string';}
- //^^ if the field is invalid
- }
- function processMany($fieldNames){
- foreach ($fieldNames as $name) {
- process($name);
- }
- }
- function processOptionalField($fieldName){
- global $vals, $validated;
- if ((isset($_POST[$fieldName])) && (strlen(trim($_POST[$fieldName])) > 0)) {
- $vals[$fieldName] = stripslashes(strip_tags($_POST[$fieldName]));
- } else {$vals[$fieldName] = 'blank';}
- }
- //here we process an example form. Demonstrates how to use the code to specify
- // the order of the fields sent out in the email.
- processMany(array('name','agency_name','address1'));
- processOptionalField('address2');
- processMany(array('City','state','zip','phone','email'));
- // prepare outgoing email:
- $body = "<table>\n";
- foreach ($vals as $name => $value ){
- $body .= "<tr><td>".$name."</td><td>".$value."</tr>\n";
- }
- $body .= "</table>";
- // echo $body; //use this to test what was just made, as well as ensure that
- // all the values were recieved nicely.
- // validate
- if (!$validated) {
- echo "<br>error in validation :-(";
- //^^ you probably want a more professional error message here
- print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html#validation\">";
- //^^ comment this out when using ajax, or to view the above debugging output
- exit;
- }
- // Connect, select database
- $link = mysql_connect($dbhost, $dbName, $dbPass)
- or die('Could not connect: ' . mysql_error());
- echo 'Connected successfully';
- mysql_select_db('db_table_name') or die('Could not select database');
- //when using a phone number, this will reduce it to an integer.
- $pattern = '/[^0-9]*/';
- $vals['number'] = preg_replace($pattern,'', $vals['number']);
- //^^make sure this refers to the correct field of course
- //TODO: jQuery and php max length.... textareas limit to 1000, etcetc
- // (don't allow too long input)
- //php is nice because it parses the $ within the string!
- $query = "INSERT INTO form_submissions VALUES ('$vals[name]','$vals[agency_name]'".
- ",'$vals[address1]','$vals[address2]','$vals[City]','$vals[state]','$vals[zip]',".
- "'$vals[phone]','$vals[email]')";
- echo "<br><br><i>".$query."</i>"; //degbugging to show what the query is.
- mysql_query($query); //add the record to the database
- mysql_close();
- //although you'll probably be setting up the db w/ phpMyAdmin, its good
- // to save the creation code for reference, if nothing else. It makes
- // it easier to change things later.
- /* temp code: mysql table creation:
- CREATE TABLE `bostonel_compliance`.`form_submissions` (
- `name` VARCHAR( 32 ) NOT NULL ,
- `agency_name` VARCHAR( 64 ) NOT NULL ,
- `address1` VARCHAR( 64 ) NOT NULL ,
- `address2` VARCHAR( 64 ) NOT NULL ,
- `City` VARCHAR( 32 ) NOT NULL ,
- `state` VARCHAR( 16 ) NOT NULL ,
- `zip` VARCHAR( 9 ) NOT NULL ,
- `phone` VARCHAR( 32 ) NOT NULL ,
- `email` VARCHAR( 64 ) NOT NULL
- ) ENGINE = MYISAM
- */
- //setup the sending of the email:
- //for the sake of trivia: you don't need this for plain text (no html) emails.
- // Its good to have it though!
- $headers = 'MIME-Version: 1.0' . "\r\n";
- $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
- $headers .= "From: $EmailFrom";
- $success = mail($EmailTo, $subject, $body, $headers);
- // redirect to success page
- if ($success){
- // echo $body; //use this for debuggin
- echo "success!";
- print "<meta http-equiv=\"refresh\" content=\"0;URL=success.html\">";
- //^^ again, unnecessary when AJAX.
- }else{
- echo "email fail!";
- print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment