Global Form in Footer

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

There is a great form plugin for Kirby: Uniform. The documentation should help to set it up.

In addition to that that, to place the form on every page, you can put the form and the controller code into a snippet.

thanks for the link, but i’m also trying to make this work just because i am trying to get better at PHP so i really want to solve it with the code i have. i did check it out and it helped a bit… here is what i have now in those 3 key lines:

require_once("site/snippets/phpmailer/class.phpmailer.php");

header('Location: ' . $page->url() . '?status=thanks');

<form method="post" action="<?php echo $page->url() ?>">

i feel like this should work. the code doesn’t break my page and if i type in: http://localhost:8888/projectname/?status=thanks - then it shows the “Thank you for subscribing!” as it should. but it won’t automatically redirect to that… if i enter an e-mail and hit subscribe, it just returns to http://localhost:8888/projectname/

any ideas?