hey everyone,
i’m trying to add a simple form to my footer, however i’m still learning PHP and i’m not sure how to make it all global (especially since i’m running it through Kirby). here is the working code i would use if it was just on a single page:
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
if ($name == "" OR $email == "") {
echo "Missing Required Fields.";
}
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE){
echo "There was a problem with the information you entered.";
exit;
}
}
if ($_POST["address"] != "") {
echo "Your form submission has an error.";
exit;
}
require_once("snippets/phpmailer/class.phpmailer.php");
//Create a new PHPMailer instance
$mail = new PHPMailer();
if (!$mail->ValidateAddress($email)) {
echo "You must enter a valid email address.";
exit;
}
$email_body = "";
$email_body = $email_body . "Name: ". $name . "<br>" ;
$email_body = $email_body . "E-Mail: " . $email . "<br>";
//Set who the message is to be sent from
$mail->setFrom($email,$name);
//Set who the message is to be sent to
$address = "company-email@gmail.com";
$mail->addAddress($address, "Northern Tech Co.");
//Set the subject line
$mail->Subject = 'Northern Tech Contact Form - ' . $name;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($email_body);
//send the message, check for errors
if (!$mail->send()) {
echo "There was an error sending the message." . $mail->ErrorInfo;
exit;
}
header("Location: contact.php?status=thanks");
exit;
} ?>
followed by this in the body:
<h1>CONTACT US</h1>
<br>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<div class="contact-col-1">
<p class="form-thanks-block">Thank you!<br>We'll be in touch shortly.</p>
</div>
<?php } else { ?>
<form method="post" action="contact.php">
<div class="contact-col-1">
<input type="text" name="name" id="name" placeholder="Name" class="form-field" required><br><br>
<input type="text" name="email" placeholder="E-mail" id="email" class="form-field" pattern="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" required><br>
<input type="text" name="address" id="address" placeholder="Address" style="display: none;">
<p style="display: none;">Please do NOT fill in the address block.</p>
<br>
</div>
<input type="submit" value="Send" class="button">
</form>
<?php } ?>
any ideas on how to make it global? from what i understand the key lines are:
require_once("snippets/phpmailer/class.phpmailer.php");
header("Location: contact.php?status=thanks");
<form method="post" action="contact.php">
any help is greatly appreciated!
thanks,
Oliver