I have a single page site with all content on it. The only other page I have is a "thanks for submitting the contact form" page. In this "thankssubmit.php" page I have my PHP form submission code.
I'm new to wordpress so I may be doing this totally wrong but I've created a custom page template for this "thankssubmit.php" page using /Template Name: Thanks Submit/.
I've then applied that template to a brand new page in WP. I can't add .php to the slug which is maybe the only problem but I can't see a way around this. A strange issue is that when I submit the form I go to the correct URL but get a "page not found" error. If I copy and paste this exact same URL I go to the correct page...
Below is the code for all.
I've had a look and I can't see this question being a repeat but if so a nudge in the right direction would be greatly appreciated!
Form
<form method="post" action="thankssubmit.php">
<h3>Drop Us a Message</h3>
<div class="row">
<div class="col-sm">
<div class="form-group">
<input id="nameInput" type="text" name="name" onkeyup="manage(this)" class="form-control" placeholder="Your Name *" />
</div>
<div class="form-group">
<input id="emailInput" type="text" name="email" onkeyup="manage(this)" class="form-control" placeholder="Your Email *" />
</div>
<div class="form-group">
<input type="text" name="phone" class="form-control" placeholder="Your Phone Number" />
</div>
<div class="form-group">
<input id="weddingDatePicker" type="date" name="date" class="form-control" placeholder="Your Wedding Date -" />
</div>
<div class="form-group">
<input type="text" name="hear" class="form-control" placeholder="How did you hear about us?" />
</div>
</div>
<div class="col">
<div class="form-group">
<textarea id="messageInput" name="message" onkeyup="manage(this)" class="form-control" placeholder="Your Message *" style="width: 100%; height: 254px;"></textarea>
</div>
<div class="form-group">
<input id="contactSubmitBtn" type="submit" disabled name="btnSubmit" class="btn" value="Send Message" />
</div>
</div>
</div>
</form>
thankssubmit.php page
<?php get_header(); ?>
<!-- THANKS OR SUCCESS MESSAGE AFTER THE EMAIL HAS BEEN SENT -->
<section id="thanksMsgPG">
<h1 class="thanksmsg">Thanks for getting in touch.</h1>
<h3 class="thanksmsg">We'll get back to you ASAP!</h3>
<a href="/"><button class="btn ">Back</button></a>
</section>
<?php get_footer(); ?>
<?php
/*
Template Name: Thanks Submit
*/
if (isset($_POST['btnSubmit'])) {
// EMAIL AND SUBJECT OF EMAIL BEING SENT
$email_to = "[email protected]";
$subject = "Contact Submission Form";
//ERROR MESSAGES IF DIED FUCNTION IS CALLED (SEMI-REDUNDANT BECAUSE SEND BUTTON WONT BE ENABLED UNTIL INPUT FIELDS ARE FILLED CORRECTLY ANYWAY)
function died($error)
{
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error . "<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// IF NOTHING ENTERED THEN THROW ERROR MESSAGE
if (!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
//GETTING THE NAME EMAIL PHONE MESSAGE FROM THE FORM AND PUTTING IT INTO VARIABLES
$full_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['phone'];
$date = $_POST['date'];
$hear = $_POST['hear']; // required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
//CHECKING TO MAKE SURE VALID EMAIL IS ENTERED
if (!preg_match($email_exp, $email_from)) {
$error_message .= 'The e-mail you entered does not appear to be valid.<br />';
}
//CHECKING TO MAKE SURE VALID NAME IS ENTERED
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $full_name)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
//CHECKING TO MAKE SURE MESSAGE IS MORE THAN 2 CHARACTERS
if (strlen($message) < 2) {
$error_message .= 'The message you entered does not appear to be valid.<br />';
}
if (strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
//MAKING SURE THERE ARE NO HEADER INJECTIONS
function clean_string($string)
{
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
//THE FORMAT OF THE EMAIL BEING SENT. CLEAN STRING CLEANING ANY WHITE SPACE
$email_message .= "Name: " . clean_string($full_name) . "\n";
$email_message .= "Email: " . clean_string($email_from) . "\n";
$email_message .= "Phone: " . clean_string($phone) . "\n";
$email_message .= "Wedding Date: " . clean_string($date) . "\n";
$email_message .= "I heard about you via: " . clean_string($hear) . "\n";
$email_message .= "Message: \r\n" . clean_string($message) . "\n";
$email_from = $full_name . '<' . $email_from . '>';
// CREATING EMAIL HEADER FOR GMAIL TO RECOGNISE
$headers = 'From: ' . $email_from . "\r\n" .
'Reply-To: ' . $email_from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $subject, $email_message, $headers);
// echo $email_message;
?>
<?php
}
?>
First image below shows the URL error after we submit the form and the second shows after we reload the pages URL.