Hello,
I am making contact form in Kirby 3.
Text type fields were easy to build because of the example.
And it works fine.
But if I create multiple checkboxes
How do I write contact.php of templates, contact.php of controllers and email.html.php?
My code is written below.
Please help me…!
Checkbox / contact.php - templates
<div class="ckb"> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle1" value="MarketingCopywriting"><label for="vehicle1">Marketing Copywriting</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle2" value="EditingProofreading"><label for="vehicle2">Editing/Proofreading</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle3" value="ScriptTranscreation"><label for="vehicle3">Script Transcreation</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle4" value="GraphicNovelTranscreation"><label for="vehicle4">Graphic Novel Transcreation</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle5" value="ArtMuseumTranscreation"><label for="vehicle5">Art & Museum Transcreation</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle6" value="InteractiveGamingTranscreation"><label for="vehicle6">Interactive/ Gaming Transcreation</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle7" value="MetadataUILocalization"><label for="vehicle7">Metadata/UI Localization</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle8" value="LegalTranslation"><label for="vehicle8">Legal Translation</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle9" value="Dubbing"><label for="vehicle9">Dubbing</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle10" value="GeneralTimedTextSubtitling"><label for="vehicle10">General Timed Text/Subtitling</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle11" value="ClosedCaptioning"><label for="vehicle11">Closed Captioning</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle12" value="TypographyLocalization"><label for="vehicle12">Typography Localization</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle13" value="DesktopPublishing"><label for="vehicle13">Desktop Publishing</label><br> <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle14" value="Other"><label for="vehicle14">Other (Please specify)</label> </div>
Other code (without checkbox) that works fine
contact.php - templates
(How do I write the checkbox code?)
<form method="post" action="<?= $page->url() ?>"> <!-- name --> <div class="field"> <label for="name"> Job Title <abbr title="required">*</abbr> </label> <input autocomplete="off" type="text" id="name" name="name" value="<?= $data['name'] ?? '' ?>" required> <?= isset($alert['name']) ? '<span class="alert error">' . html($alert['name']) . '</span>' : '' ?> </div> </form>
contact.php - controllers
(How do I write the checkbox code?)
<?php return function($kirby, $pages, $page) { $alert = null; if($kirby->request()->is('POST') && get('submit')) { // check the honeypot if(empty(get('website')) === false) { go($page->url()); exit; } $data = [ 'name' => get('name'), 'company' => get('company'), 'email' => get('email'), 'text' => get('text'), 'city' => get('city'), 'state' => get('state'), 'postalcode' => get('postalcode'), 'country' => get('country'), 'when' => get('when'), 'howlong' => get('howlong'), 'source' => get('source'), 'target' => get('target') ]; $rules = [ 'name' => ['required', 'min' => 3], 'email' => ['required', 'email'], 'text' => ['required', 'min' => 3, 'max' => 3000], ]; $messages = [ 'name' => 'Please enter a valid name', 'email' => 'Please enter a valid email address', 'text' => 'Please enter a text between 3 and 3000 characters' ]; // some of the data is invalid if($invalid = invalid($data, $rules, $messages)) { $alert = $invalid; // the data is fine, let's send the email } else { try { $kirby->email([ 'template' => 'email', 'from' => 'myemail@gmail.com', 'replyTo' => $data['email'], 'to' => 'myemail@gmail.com', 'subject' => esc($data['name']) . ' sent you a message from your contact form', 'data' => [ 'name' => esc($data['name']), 'company' => esc($data['company']), 'email' => esc($data['email']), 'city' => esc($data['city']), 'state' => esc($data['state']), 'postalcode' => esc($data['postalcode']), 'country' => esc($data['country']), 'text' => esc($data['text']), 'sender' => esc($data['company']), 'when' => esc($data['when']), 'howlong' => esc($data['howlong']), 'source' => esc($data['source']), 'target' => esc($data['target']) ] ]); } catch (Exception $error) { $alert['error'] = "The form could not be sent"; } // no exception occured, let's send a success message if (empty($alert) === true) { $success = 'Your message has been sent, thank you. We will get back to you soon!'; $data = []; } } } return [ 'alert' => $alert, 'data' => $data ?? false, 'success' => $success ?? false ]; };
email.html.php
(How do I write the checkbox code?)
<p><b>Job Title</b><br><?= $name ?></p> <p><b>Company</b><br><?= $company ?></p> <p><b>Email</b><br><?= $email ?></p> <p><b>City</b><br><?= $city ?></p>