I have created a contact us form which sends an email when submitted. While the email triggers successfully, the submission leads to a page with my index.php template without any change in the URL. It might have to do with the template hierarchy, but I've tried all other methods mentioned, and none of them have worked. Here's my code for the contact us page with the form.
<?php /* Template Name: Contact Us */ ?>
<?php get_header(); ?>
<?php
$name = $_POST['name'];
$from = $_POST['email'];
$message = $_POST['message'];
$subject = "Message for Aria Event Planners from '$name' ";
$to = "[email protected]";
$headers = "MIME-Version : 1.0" . "\r\n";
$headerx .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = "From: <$from> \r\n";
?>
<div class="container">
<div class="header for-text-layout">Contact Us </div>
<div class="for-text-layout p-3">
<div></div>
<form method="POST">
<p>
<label>Name</label>
<input type="text" name="name" placeholder="Your Name" class="input" id="name" required>
</p>
<p>
<label>E-Mail Address </label>
<input type="email" name="email" placeholder="Your E-Mail" class="input" required>
</p>
<p class="message">
<label>Message </label>
<textarea rows="3" cols="30" class="input" name="message" placeholder="Your Message Here" required></textarea>
</p>
<p class="submit">
<button type="submit" name="submit" id="submit">Send Message</button>
</p>
<div>
<?php
if(isset($_POST['submit'])){
if(mail($to, $subject, $message, $headers)){
get_site_url('/email-success/');
} else {
get_site_url('/email-failure/');
}
}
?>
</div>
<div></div>
</form>
</div>
</div>
<?php get_footer(); ?>