Customing the contact form - problem : can't add data

Hi everyone,

I’ve got a problem concerning Kirby mail contact form.

I want to create a custom form. I followed the instructions on the dedicated page.
It works until I try to add new fields. I’ve added it into the contact.php, the controller (also named contact.php but in the controllers folder), the email.php and email.html.php.
The form is correctly displayed but I get an error when I click on the submit button. It works again if I remove the four lines I added to the controller, these ones (bolded) :
$data = [
’tel’ => get(‘tel’),
’pers’ => get(‘pers’),

and these ones (bolded) :
$kirby->email([
‘data’ => [
’tel’ => esc($data[‘tel’]),
’pers’ => est($data[‘pers’]),

Any idea ?

Here is the full code of my controller :

<?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 = [
            'tel'   => get('tel'),
            'pers'  => get('pers'),
            'name'  => get('name'),
            'email' => get('email'),
            'text'  => get('text'),
        ];

        $rules = [
            'name'  => ['required', 'min' => 3],
            'email' => ['required', 'email'],
            'text'  => ['required', 'min' => 3, 'max' => 3000],
        ];

        $messages = [
            'name'  => 'Entrez un nom valide svp',
            'email' => 'Entrez une adresse valide svp',
            'text'  => '3000 caractères maximum'
        ];

        // 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'     => 'contact@lacuverieduchateau.com',
                    'replyTo'  => $data['email'],
                    'to'       => 'adrien.payet@outlook.com',
                    'subject'  => esc($data['name']) . ' a envoyé un message via le site internet',
                    'data'     => [
                        'tel'    => esc($data['tel']),
                        'pers'   => est($data['pers']),
                        'text'   => esc($data['text']),
                        'sender' => esc($data['name']),
                    ]
                ]);

            } catch (Exception $error) {
                $alert['error'] = "Le formulaire n'a pas pu être envoyé";
            }

            // no exception occured, let's send a success message
            if (empty($alert) === true) {
                $success = 'Votre message a bien été envoyé, merci. Nous vous répondons dès que possible.';
                $data = [];
            }
        }
    }

    return [
        'alert'   => $alert,
        'data'    => $data ?? false,
        'success' => $success ?? false
    ];
};

What is the error and what is the code in your template?

Thx for your answer @bvdputte.

I get the common generic error :
This page is currently offline due to an unexpected error. We are very sorry for the inconvenience and will fix it as soon as possible.

Here is my template :

<?php if($success): ?>
<div class="alert success">
    <p><?= $success ?></p>
</div>
<?php else: ?>
<?php if (isset($alert['error'])): ?>
    <div><?= $alert['error'] ?></div>
<?php endif ?>
<form method="post" action="<?= $page->url() ?>">
    <div class="field">
        <label for="name">
            Nom <abbr title="required">*</abbr> :
        </label>
        <input type="text" id="name" name="name" value="<?= $data['name'] ?? '' ?>" required>
        <?= isset($alert['name']) ? '<span class="alert error">' . html($alert['name']) . '</span>' : '' ?>
    </div>
    <div class="field">
        <label for="email">
            Email <abbr title="required">*</abbr> :
        </label>
        <input type="email" id="email" name="email" value="<?= $data['email'] ?? '' ?>" required>
        <?= isset($alert['email']) ? '<span class="alert error">' . html($alert['email']) . '</span>' : '' ?>
    </div>
    <div class="field">
        <label for="tel">
            Téléphone :
        </label>
        <input type="number" id="tel" name="tel" value="<?= $data['tel'] ?? '' ?>">
        <?= isset($alert['tel']) ? '<span class="alert error">' . html($alert['tel']) . '</span>' : '' ?>
    </div>
    <div class="field">
        <label for="pers">
            Nombre de personnes :
        </label>
        <input type="number" id="pers" name="pers" value="<?= $data['pers'] ?? '' ?>">
        <?= isset($alert['pers']) ? '<span class="alert error">' . html($alert['pers']) . '</span>' : '' ?>
    </div>

    <div class="field">
        <label for="text">
            Message <abbr title="required">*</abbr> :
        </label>
        <textarea id="text" name="text" required>
            <?= $data['text']?? '' ?>
        </textarea>
        <?= isset($alert['text']) ? '<span class="alert error">' . html($alert['text']) . '</span>' : '' ?>
    </div>
    <input type="submit" name="submit" value="Envoyer">
</form>
<?php endif ?>

You should use variable directly like $pers instead of $data['pers'].

Thx a lot !

In the template ? in the controller ? both ?
Could you explain me why on ‘pers’ and not on ‘tel’ ?

Usage on controller is correct :+1:

You should directly use all the indexes in the data array in the template: $tel, $pers, $text, $sender

Sorry but I’m a newbie and I don’t understand precisely. Could you be more specific ?

You mean in the value field ? I should write “$pers” instead of “$data[‘pers’]” ?

Controller

You used correct, no needed any change.

$kirby->email([
    'template' => 'email',
    'from'     => 'contact@lacuverieduchateau.com',
    'replyTo'  => $data['email'],
    'to'       => 'adrien.payet@outlook.com',
    'subject'  => esc($data['name']) . ' a envoyé un message via le site internet',
    'data'     => [
        'tel'    => esc($data['tel']),
        'pers'   => est($data['pers']),
        'text'   => esc($data['text']),
        'sender' => esc($data['name']),
    ]
]);

Template

Correct

<input type="number" id="tel" name="tel" value="<?= $tel ?? '' ?>">

Wrong

<input type="number" id="tel" name="tel" value="<?= $data['tel'] ?? '' ?>">

Because the content of the data index is sent directly to the template:


Yes, exactly!

thx again, I’m afraid it doesn’t work.

Maybe I’m missing something but the problem seems to comes from the controller:
first because the error comes after submitting the form
second because the form works when I remove the 4 lines I added, namely :
$data = [
’tel’ => get(‘tel’),
’pers’ => get(‘pers’),

and these ones (bolded) :
$kirby->email([
‘data’ => [
’tel’ => esc($data[‘tel’]),
’pers’ => est($data[‘pers’]),

Hmm at first, there is no est() function on est($data['pers'])

Should I also write “$email” instead of "$data[‘email’] too ? The code given in the Kirby guide uses the second option.

Oh yes my mistake ! Let’s try again

With debug mode enabled, do you get any errors?

It works ! The problem was coming from my mistake : “est()” instead of “esc()”

Thx a lot @ahmetbora !

Great :+1: Don’t forget about usage of variables in template.

I’ll check carefully.

Thx again !

Please choose solution answer and mark the question as solved :ok_hand:

Done.

Have a nice day @ahmetbora