How do I send back the failure and success page?
Can I just load a new body onto the page?
I have templates for failure and success but I am not sure how to load them onto my page.
<?php
//this will load the mustache template library
require_once 'mustache/mustache/src/Mustache/Autoloader.php';
Mustache_Autoloader::register();
// this will create a new mustache template engine
$mustache = new Mustache_Engine;
// these lines load your header, footer, and body template into strings
$header = file_get_contents('templates/header.html');
$body = file_get_contents('templates/emailform.html');
$footer = file_get_contents('templates/footer.html');
// this will be used to send the page title into the page
$header_data = ["pagetitle" => "Contact"];
//this is being used to send a footer title and local time to the footer
$footer_data = [
"localtime" => date('l jS \of F Y h:i:s A'),
"footertitle" => "Contact"];
echo $mustache->render($header, $header_data) . PHP_EOL;
echo $mustache->render($body) . PHP_EOL;
echo $mustache->render($footer, $footer_data) . PHP_EOL;
// up to here I am loading my contact form page
function successpage() {
/* ********************************************
* HOW TODO THIS (erase this comment when you are done)
* ********************************************
* write your success page here. you need to use templating
* so you need to add the mustache library above where indicated.
* Write this part to send back a page.
*/
}
function failurepage() {
/* ********************************************
* HOW TODO THIS (erase this comment when you are done)
* ********************************************
* write your failure page here. you need to use templating
* so you need to add the mustache library above where indicated.
* Write this part to send back a page.
*/
}
function main() {
/* This will test to make sure we have a non-empty $_POST from
* the form submission. */
if (!empty($_POST)) {
/* ********************************************
* I need TODO this part as well, but I am first trying to send back the page first.
* ********************************************
* Do your validation and cleaning here. You need to extract FOUR
* things from the $_POST array...
$name --> trim it, strip HTML tags, and sub-string it to 64
$subject --> trim it, strip HTML tags, and sub-string it to 64
$message --> trim it, strip HTML tags, and sub-string it to 1000
$from --> look up and use the PHP filter_var() with FILTER_VALIDATE_EMAIL
https://www.php.net/manual/en/function.filter-var.php
*
*/
if (!empty($name) && !empty($from) && !empty($subject) && !empty($message)) {
/* this forms the correct email headers to send an email */
$headers = "From: $from\r\n";
$headers .= "Reply-To: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
if (mail(/****fill out the paramters here****/)) {
successpage();
} else {
failurepage();
}
} else {
failurepage();
}
} else {
failurepage();
}
}
// this kicks off the script
main();