How do I create multiple checkboxes for email?

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&nbsp;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>

Form (shortened)

 <div class="ckb">
     <input type="checkbox" name="type[]" id="vehicle1" value="Marketing" <?= isset($data['type']) && in_array('Marketing', $data['type']) ? 'checked' : '' ?>>
     <label for="vehicle1">Marketing</label><br>
     <input type="checkbox" name="type[]" id="vehicle2" value="Editing"  <?= isset($data['type']) && in_array('Editing', $data['type']) ? 'checked' : '' ?>>
     <label for="vehicle2">Editing</label><br>
     <input type="checkbox" name="type[]" id="vehicle3" value="Transcreation"  <?= isset($data['type']) && in_array('Transcreation', $data['type']) ? 'checked' : '' ?>>
     <label for="vehicle3">Transcreation</label><br>
 </div>

Controller:

 $data = [
     'name'      => get('name'),
     'type'      => get('type'),
 ];

$data['type'] will return an array.

Email template:

<p><b>Type</b><br><?= implode(',', $type) ?></p>
1 Like

Thank you for the reply.
but the checkbox still doesn’t work.(The other forms work.)

Controller (shortened)

 $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'),
            'type'   => get('type') /****** here ******/
        ];

 if (empty($alerts)) {
            try {
                $kirby->email([
                    'template' => 'email',
                    'from'     => 'mymail@gmail.com',
                    'replyTo'  => $data['email'],
                    'to'       => 'mymail@gmail.com',
                    'subject'  => esc($data['name']) . ' a message from website',
                    'data'     => [
                        'name'  => esc($data['name']),
                        'type' => esc($data['type']), /****** here ******/
                        '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'])
                    ],
                    'attachments' => $attachments
                ]);

Email template:

<?php
?>
<p><b>Job Title</b>
<?= $name ?></p>
<p><b>Company</b>
<?= $company ?></p>
<p><b>Email</b>
<?= $email ?></p>
<p><b>City</b>
<?= $city ?></p>
<p><b>State/Province</b>
<?= $state ?></p>
<p><b>Zip/Postal Code</b>
<?= $postalcode ?></p>
<p><b>Country</b>
<?= $country ?></p>
<p><b>Type</b><br><?= implode(',', $type) ?></p> /****** here ******/
<p><b>How long is your file (or asset)?</b>
<?= $howlong ?></p>
<p><b>Source Language</b>
<?= $source ?></p>
<p><b>Target Language</b>
<?= $target ?></p>
<p><b>When do you need it by?</b>
<?= $when ?></p>
<p><b>Please describe your file</b>
<?= $text ?></p>
<p>Best,</p>
<p><?= $sender ?></p>

Dear texnixe

The code you told me doesn’t work.
Perhaps I wrote the templates file incorrectly.
But I don’t know what the problem is.

If you don’t mind, can you take a look?
Please help me ! :’(

<form method="post" action="<?= $page->url() ?>" enctype="multipart/form-data">
    <div class="field">
        <label for="checkbox" id="checkbox">
               What type of service do you need?
               Select all that apply.
                    <abbr title="required">*</abbr>
         </label>
              <!-- checkbox -->
              <div class="ckb">
                  <input autocomplete="off" class="checkbox" type="checkbox" name="type[]" id="vehicle1" value="MarketingCopywriting"><label for="vehicle1">Marketing&nbsp;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>
              </div>
      </div>
</form>

“Doesn’t work” is a bit unspecific. Could you please tell us what exactly doesn’t work?

After selecting the check box and pressing the submit button, an error message appears. “The form could not be sent”

catch (Exception $error) {
            $alert['error'] = "<span>The form could not be sent</span>";
        }

And the error message only appears when I write the code associated with the checkbox.

No other features are seen…

This code is incorrect. You cannot use esc() with an array, only with a string.

Oh! I understand now. It works !!! :sob: :heart:
Thank you very much for saving me :heart: :heart: :speak_no_evil: