Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
title: Contact
preset: page
icon: 📮
status:
draft: Draft
listed: Published
fields:
to:
label: Receiver (To)
type: email
placeholder: your@email.ltd
help: The email address that should be the receiver of the emails.
width: 1/2
from:
label: Sender (From)
type: email
placeholder: contact@domain.ltd
help: The email address that will be the sender of the emails.
width: 1/2
sidebar:
meta:
type: fields
fields:
subjects:
type: structure
fields:
subject:
type: text
<?php
use Uniform\Form;
return function ($kirby, $page) {
$form = new Form([
'name' => [
'rules' => ['required'],
'message' => 'Please enter a valid name',
],
'email' => [
'rules' => ['required', 'email'],
'message' => 'Please enter a valid email address',
],
'subject' => [],
'message' => [
'rules' => ['required'],
'message' => 'Please enter a message',
],
]);
if ($kirby->request()->is('POST')) {
$to = $page->to()->value();
$from = $page->from()->value();
$form->emailAction([
'to' => $to,
'from' => $from,
'subject' => '{{subject}} - from {{name}}.',
'template' => 'contact',
]);
}
return compact('form');
};
<?php snippet('header') ?>
<main class="main" role="main">
<?php if ($form->success()): ?>
<div class="alert success">
—— Success message ——
</div>
<?php else: ?>
<form action="<?php echo $page->url() ?>" method="POST" class="inquire-form">
<div class="tfields">
<div class="field<?php if ($form->error('name')): ?> error<?php endif; ?>">
<input name="name" type="text" value="<?php echo $form->old('name') ?>" placeholder="Name">
</div>
<div class="field<?php if ($form->error('email')): ?> error<?php endif; ?>">
<input name="email" type="email" value="<?php echo $form->old('email') ?>" placeholder="Email">
</div>
</div>
<?php if($page->subjects() != ''): ?>
<div class="field select">
<?php $value = $form->old('subject') ?>
<select name="subject" id="subject" class="required">
<optgroup label="Subjects">
<?php foreach ($page->subjects()->yaml() as $subject): ?>
<option value="<?= $subject['subject'] ?>" <?php e($value==$subject['subject'], ' selected')?>><?= $subject['subject'] ?></option>
<?php endforeach ?>
</optgroup>
</select>
</div>
<?php else: ?>
<div class="honeypot">
<input type="subject" id="subject" name="subject" value="New message from your website.">
</div>
<?php endif ?>
<div class="field<?php if ($form->error('message')): ?> error<?php endif; ?>">
<textarea name="message" placeholder="Message"><?php echo $form->old('message') ?></textarea>
</div>
<?php echo csrf_field() ?>
<?php echo honeypot_field() ?>
<div class="field">
<input type="submit" name="submit" value="Submit">
</div>
</form>
<?php endif ?>
</main>
<?php snippet('footer') ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment