Created
February 6, 2014 18:56
-
-
Save mantismamita/8850383 to your computer and use it in GitHub Desktop.
Basic form with php validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The template used for displaying the queries form | |
* | |
* @package hostel_mama | |
*/ | |
?> | |
<?php | |
$error_query_name = false; | |
$error_query_email = false; | |
$error_query_phone = false; | |
$error_date_res = false; | |
$error_res_num = false; | |
$error_query = false; | |
if (isset($_POST['queries-submit'])) { | |
// Initialize the variables | |
$query_name = ''; | |
$query_email = ''; | |
$query_phone = ''; | |
$date_res = ''; | |
$res_num = ''; | |
$query = ''; | |
if (trim($_POST['query-name']) === '') { | |
$error_query_name = true; | |
} else { | |
$query_name = trim($_POST['query-name']); | |
} | |
if (trim($_POST['query-email']) === '' || !isEmail($_POST['query-email'])) { | |
$error_query_email = true; | |
} else { | |
$query_email = trim($_POST['query-email']); | |
} | |
if (trim($_POST['query-phone']) === '') { | |
$error_query_phone = true; | |
} else { | |
$query_phone = trim($_POST['query-phone']); | |
} | |
if (trim($_POST['date-res']) === '0') { | |
$error_date_res = true; | |
} else { | |
$date_res = trim($_POST['date-res']); | |
} | |
if (trim($_POST['res-num']) === '') { | |
$error_res_num = true; | |
} else { | |
$res_num = stripslashes(trim($_POST['res-num'])); | |
} | |
if (trim($_POST['query']) === '') { | |
$error_query = true; | |
} else { | |
$query = trim($_POST['query']); | |
} | |
// Check if we have errors | |
if (!$error_query_name && !$error_query_email && !$error_query_phone && !$error_date_res && !$error_res_num && !$error_query) { | |
// Get the receiver email from the WP admin panel | |
// change after initial testing! | |
$receiver_email = get_field('queries_e-mail'); | |
$subject = "Message de $query_name"; | |
$body = "Vous avez un nouveau message de la part de $query_name" . PHP_EOL; | |
$body .= "Date de réservation: $date_res" . PHP_EOL; | |
$body .= "Numéro de réservation: $res_num" . PHP_EOL; | |
$body .= "Vous pouvez le contacter $query_name via email à $query_email or par téléphone à $query_phone.". PHP_EOL; $body .= PHP_EOL; | |
$body .= "Message: $query" . PHP_EOL . | |
$body .= PHP_EOL; | |
$headers = "From: $query_email" . PHP_EOL; | |
$headers .= "Reply-To: $query_email" . PHP_EOL; | |
$headers .= "MIME-Version: 1.0" . PHP_EOL; | |
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL; | |
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL; | |
// If all is good, we send the email | |
if (mail($receiver_email, $subject, $body, $headers)) { | |
$query_email_sent = true; | |
} else { | |
$query_email_sent_error = true; | |
} | |
} | |
} | |
?> | |
<?php if (isset($query_email_sent) && $query_email_sent == true) : ?> | |
<div data-alert class="alert-box success"> | |
<p><?php _e('Thank you.', 'hostel_mama' );?></p> | |
<p><?php _e('We will respond to your message as quickly as possible.', 'hostel_mama' );?></p> | |
</div> | |
<?php elseif (isset($query_email_sent_error) && $query_email_sent_error == true) : ?> | |
<div data-alert class="alert-box alert"> | |
<p><?php _e('There was an error!', 'hostel_mama' );?></p> | |
<p><?php _e('Unfortunately, there was an error while trying to send the email. Please try again.', 'hostel_mama' );?></p> | |
</div> | |
<?php else : | |
$url= 'index.php?page_id=10'; | |
$string= sprintf(__('Please check our <a href="%1$s"><abbr title="Frequently asked questions">FAQ </abbr> page</a> before submitting this form. You will likely find the answer there.', 'hostel_mama'), esc_url( $url ) ); | |
?> | |
<div class="panel"> | |
<p><?php echo $string ?></p> | |
</div> | |
<form method="post" action="<?php the_permalink(); ?>" data-abide> | |
<fieldset> | |
<div class="row"> | |
<div class="large-6 columns"> | |
<label for="query-name"><?php _e('Full name: ', 'hostel_mama' )?></label> | |
<input id="query-name" name="query-name" type="text" required pattern="[a-zA-Z]+"/> | |
<small class="error"><?php _e('Name is required and must be composed of letters.', 'hostel_mama' )?></small> | |
<label for="query-email"><?php _e('Email address: ', 'hostel_mama' )?></label> | |
<input id="query-email" name="query-email" type="email" required /> | |
<small class="error"><?php _e('An email address is required.', 'hostel_mama' )?></small> | |
<label for="query-phone"><?php _e('Phone: ', 'hostel_mama' )?></label> | |
<input id="query-phone" name="query-phone" type="tel" /> | |
<div class="row"> | |
<div class="large-6 columns"> | |
<label for="date-res"><?php _e('Date of stay or reservation: ', 'hostel_mama' )?></label> | |
<input id="date-res" name="date-res" type="date" /> | |
</div> | |
<div class="large-6 columns"> | |
<label for="res-num"><?php _e('Reservation number: ', 'hostel_mama' )?></label> | |
<input id="res-num" name="res-num" type="number" /> | |
</div> | |
</div> | |
</div> | |
<div class="large-6 columns"> | |
<label for="query"><?php _e('Message or Query: ', 'hostel_mama' )?></label> | |
<textarea id="query" name="query" class="lrg-txtarea" cols="30" rows="10"></textarea> | |
<small class="error"><?php _e('You forgot to include a message.', 'hostel_mama' )?></small> | |
</div> | |
<input type="hidden" id="queries-submit" name="queries-submit" value="true" /> | |
<input type="submit" class="button success radius"> | |
<input type="reset" class="button alert radius"/> | |
</div> </fieldset> | |
</form> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment