public
Created

Basic php form

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
<?php
/* Probably needs some basic cleanup and functionality stuff before it's actually usable. Created to eventually add form validation stuff etc. */
$nameError = '';
$emailError = '';
$commentError = '';
$locationError = '';
$companyError = '';
$expertiseError = '';
$projsizeError = '';
$willorwontError = '';
$devlevelError = '';
$costError = '';
$timeframeError = '';
if(isset($_POST['submitted'])) {
if(trim($_POST['contactName']) === '') {
$nameError = 'Please enter your name.';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
if(trim($_POST['email']) === '') {
$emailError = 'Please enter your email address.';
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
if(trim($_POST['comments']) === '') {
$commentError = 'Please enter a message.';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
if(trim($_POST['location']) === '') {
$locationError = 'Please enter your location.';
$hasError = true;
} else {
$location = trim($_POST['location']);
}
if(trim($_POST['company']) === '') {
$companyError = 'Please enter your company.';
$hasError = true;
} else {
$company = trim($_POST['company']);
}
 
$expertise = trim($_POST['expertise']);
$projsize = trim($_POST['projsize']);
$willorwont = trim($_POST['willorwont']);
$devlevel = trim($_POST['devlevel']);
$cost = trim($_POST['cost']);
$timeframe = trim($_POST['timeframe']);
 
if(!isset($hasError)) {
$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = get_option('admin_email');
}
$subject = '[Contact Form] From '.$name;
$body = "Hi Grace, it's great to meet you, my name is $name and my email is $email. I'm based in $location and my company is called $company. I'd like to talk about working together, I need your expertise in $expertise. The project size is a $projsize and $willorwont require $devlevel. Considering the project's size and scope, the budget is set at approximately $cost. I have a launch date of $timeframe in mind. Here's some further information about the project: $comments";
$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
 
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
 
} ?>
 
<?php if(isset($emailSent) && $emailSent == true) { ?>
<div class="thanks">
<p>Thanks, your email was sent successfully.</p>
</div>
<?php } else { ?>
<?php if(isset($hasError) || isset($captchaError)) { ?>
<p class="error">Sorry, an error occurred.<p>
<?php } ?>
 
<form action="" id="contactForm" method="post">
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" />
<?php if($nameError != '') { ?>
<span class="error"><?php echo $nameError; ?></span>
<?php } ?>
<input type="email" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" />
<?php if($emailError != '') { ?>
<span class="error"><?php echo $emailError; ?></span>
<?php } ?>
<select name="expertise" id="expertise">
<option>Website Redesign</option>
</select>
<textarea name="comments" id="commentsText"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
<input type="hidden" name="submitted" id="submitted" value="true" />
<button id="submit" type="submit"><?php _e('Send Email', 'framework') ?></button>
</form>
<?php } ?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.