Two contact forms - separate controllers

Hey all

On the site I’m working on I have two contact forms. One on the Contract page, and one in the footer as a ‘quick contact’.

When I send an email from one of them, both of them show the ‘successful sent’ message. Is there a way to separate those?

The contact from I built is from the cookbook recipe.

Hey

you could include a hidden field in your form for the origin of the form itself.

<input type="hidden" name="origin" value="contact or footer">

in your controller

if (empty($alert) === true) {
    if(get('origin') == 'contact' {
        $success = 'Your message has been sent, thank you. We will get back to you soon!';
    }
    else {
        $success_footer = 'Your message has been sent, thank you. We will get back to you soon!';
    }
    $data = [];
}

and

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

after that, you can edit your if-statement in your template and/or snipper

<?php if($success): ?>

or

<?php if($success_footer): ?>

i hope this is kinda understandable

1 Like

Thanks David. In the original message I forgot to mention that I also receive two emails, one from each form, although I only fill one out. Would this workaround fix that too?

Would be interesting to see your setup… Are you handling both forms from the same controller? Are the forms identical, resulting in two exactly the same forms on the contact page?

I have two controllers, one for the contact page, one for footer, the forms are identical, and yes when you open the contact page, one is in the body, one in the footer. Probably not the best way around it - could I get away with just site controller?

What are these two controllers? If the form is sent twice, then it seems that one form goes to two controllers.

Still missing information…

I have a site.php controller which covers the form in the footer, and then I have a contact.php controller which covers the form on the contact page.

I just removed the contact.php controller, and the form still works so I think I will spend a bit more time on it to get it working properly.

I think I tested the form too many times and now the emails don’t come through anymore, is there a way to clear cache somehow?

The code used in the controller is from the cookbook contact form recipe.

So this partially works. If I send from contact, then it shows success only on contact page. If I send from footer, then it shows success text on both.

Here is my code

<?php
return function($kirby, $site, $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'),
            'email' => get('email'),
            'text'  => get('text')
        ];

        $rules = [
            'name'  => ['required', 'minLength' => 3],
            'email' => ['required', 'email'],
            'text'  => ['required', 'minLength' => 3, 'maxLength' => 3000],
        ];

        $messages = [
            'name'  => 'Unesite Vaše ime',
            'email' => 'Unesite validnu email adresu',
            'text'  => 'Poruka bi trebala da sadrži izmedju 3 i 3000 karaktera'
        ];

        $from = new \Kirby\Cms\User([
            'email' => 'info@radioritamsrca.com',
            'name' => 'Ritam Srca Portal',
        ]);


        // 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'     => $from,
                    'replyTo'  => $data['email'],
                    'to'       => 'mkolundzic@gmail.com',
                    'subject'  => esc($data['name']) . ' vam je poslao poruku sa Ritam Srca portala',
                    'data'     => [
                        'text'   => esc($data['text']),
                        'sender' => esc($data['name'])
                    ]
                ]);

            } catch (Exception $error) {
                if(option('debug')):
                    $alert['error'] = '<p class="text-alert my-4 fs-5 fw-semibold">Email nije uspešno poslat: ' . $error->getMessage() . '</p>';
                else:
                    $alert['error'] = '<p class="text-alert my-4 fs-5 fw-semibold">Email nije uspešno poslat!</p>';
                endif;
            }

            // no exception occurred, let's send a success message
            if (empty($alert) === true) {

                if(get('origin') == 'contact') {
                    $success = '<p class="text-success fs-5 fw-semibold">Hvala vam, Vaša poruka je poslata.<br>Odgovorićemo na Vašu poruku u najkraćem mogućem roku.</p>';
                }
                else {
                    $success_footer = '<p class="text-success fs-5 fw-semibold">Hvala vam, Vaša poruka je poslata.<br>Odgovorićemo na Vašu poruku u najkraćem mogućem roku.</p>';
                }

                $data = [];
            }
        }
    }

    // fetch latest blog posts
        $recent = page('magazin')->children()->listed()->flip();
        // fetch all tags
        $tags = page('magazin')->children()->pluck('tags', ',', true);
        //shufle tags
        $tagshufled = (A::shuffle($tags));

    return [
        'alert'   => $alert,
        'data'    => $data ?? false,
        'success' => $success ?? false,
        'success_footer' => $success_footer ?? false,
        'recent'  => $recent,
        'tags'  => $tags,
        'tagshufled'  => $tagshufled
    ];
};
?>

contact template

<?php if($success): ?>
                                <p class="text-success"><?= $success ?></p>
                            <?php else: ?>
                            <?php if (isset($alert['error'])): ?>
                                <div><?= $alert['error'] ?></div>
                            <?php endif ?>
                            <form method="post" action="<?= $page->url() ?>" class="form-style">
                                <div class="honeypot">
                                    <label for="vas-website">Website <abbr title="required">*</abbr></label>
                                    <input type="url" id="vas-website" name="website" tabindex="-1">
                                </div>
                                    <input type="hidden" name="origin" value="contact">
                                    <input type="text" id="vase-ime" name="name" value="<?= esc($data['name'] ?? '', 'attr') ?>" placeholder="Vaše ime" aria-label="Vaše ime" required>
                                    <?= isset($alert['name']) ? '<p class="text-alert fw-semibold">' . esc($alert['name']) . '</p>' : '' ?>

                                    <input type="email" id="vas-email" name="email" value="<?= esc($data['email'] ?? '', 'attr') ?>" placeholder="Vaš e-mail" aria-label="Vaš e-mail" required>
                                    <?= isset($alert['email']) ? '<p class="text-alert fw-semibold">' . esc($alert['email']) . '</p>' : '' ?>

                                    <textarea id="vasa-poruka" name="text" rows="5"  placeholder="Vaša poruka" aria-label="Vaša poruka" required><?= esc($data['text'] ?? '') ?></textarea>
                                    <?= isset($alert['text']) ? '<p class="text-alert fw-semibold">' . esc($alert['text']) . '</p>' : '' ?>

                                <input type="submit" id="posalji-email" name="submit" value="Pošalji" class="btn-red rounded" title="Pošalji E-mail radiju Ritam Srca" aria-label="Pošalji E-mail radiju Ritam Srca" >
                            </form>
                            <?php endif ?> 

footer

                <?php if($success_footer): ?>
                    <p class="text-success"><?= $success_footer ?></p>
                <?php else: ?>
                <?php if (isset($alert['error'])): ?>
                    <p><?= $alert['error'] ?></p>
                <?php endif ?>
                    <form method="post" action="<?= $page->url() ?>" class="form-style">
                        <div class="honeypot">
                            <label for="website">Website <abbr title="required">*</abbr></label>
                            <input type="url" id="website" name="website" tabindex="-1">
                        </div>
                            <input type="text" id="name" name="name" value="<?= esc($data['name'] ?? '', 'attr') ?>" placeholder="Ime" >
                            <?= isset($alert['name']) ? '<p class="text-alert fw-semibold">' . esc($alert['name']) . '</p>' : '' ?>

                            <input type="email" id="email" name="email" value="<?= esc($data['email'] ?? '', 'attr') ?>" placeholder="E-mail" >
                            <?= isset($alert['email']) ? '<p class="text-alert fw-semibold">' . esc($alert['email']) . '</p>' : '' ?>

                            <textarea id="text" name="text" rows="3" placeholder="Poruka" ><?= esc($data['text'] ?? '') ?></textarea>
                            <?= isset($alert['text']) ? '<p class="text-alert fw-semibold">' . esc($alert['text']) . '</p>' : '' ?>
                        <input type="submit" name="submit" value="Pošalji" class="button btn-red rounded" title="Pošalji E-mail radiju Ritam Srca">
                    </form>
                <?php endif ?> 

Hey,

have you tried to add the origin field with value “footer” in the footer form?
it is missing there

Yes it is, is it meant to be there too?