Contact form controller with I18n::template

Hello,

I am building a multi-language website and I am using a lot <?= I18n::template(XXX', null, []);?>.
This works throughout my templates but I can’t get it to work in a controller which I created for a mail-form followed by your guide https://getkirby.com/docs/cookbook/forms/basic-contact-form.
I have read a more or less good solution in this forum but I don’t need to adjust such things in a panel.
How can I access my language files in a controller?

Thank you

What exactly are you doing in the controller?

It’s the controller which you present in your tutorial.
But as the text, I mean errors and messages, is hard coded in there I would like to put all of this in my language php’s.

And where exactly is the problem? Or the error message?

If I try the following, the mail is sent but I don’t get no message.

if (empty($alert) === true) {
                $success = I18n::template('message_success', null, []);
                $data = [];
            }

I found the issue. I missed to define the variable in my default language and just did it in the other language files.

That would have been my next question, if the variable is defined at all.

BTW, if you don’t want to replace anything in your string, you might as well just use the t() helper.

t('my_variable')

Oh, this is handy. Thank you for this.

BTW, as we are already in here.
How it comes that when I have this contact form way down on my page and I press “send” the page reloads and the site jumps to the top.
What do I have to do to stay where I am? Or is this a more complex issue?

It’s standard behavior when sending a form, it’s similar to a page reload.

The alternative would be to handle form submissions via Ajax.

Got it. I will dive into it :slight_smile:

Thank you for your help once again. I really enjoy working with Kirby.
For this project I first started off with Wordpress but it is overloaded nowadays and my special needs couldn’t be satisfied with it. But with Kirby it feels like being on a rocket ship.

Ok. I am lost.
I started of without AJAX like this:

  1. I have a snippet which contains a contact form. This snippet I use on various pages.
  2. Because I use this snippet on various pages, I use the site.php controller for handling the submit-data

This worked so far and now I wanted to implement an ajax-request to stop the page refresh.

The new snippet contains a script at the end like the following but I don’t know what url I have to pass.

  1. Do I have to pass site.php? But how?
  2. Or can I use an individual controller for my snippet? But how would I call it?

My rough Snippet

<form method="post">
<!-- -->
</form>
<script>
        let contactForm = document.querySelector('form');

        const handleForm = async (e) => {
            var formData = {
                name: $("#name").val(),
                email: $("#email").val(),
                text: $("#text").val(),
            };
            e.preventDefault();
            $.ajax({
                    type: "POST",
                    url: ???,
                    data: formData,
                    dataType: "json",
                    encode: true,
                })
                .done(function(data) {
 
// ... //

                    )
                .fail(function(data) {

// ... //
                });
        }
        contactForm.addEventListener('submit', handleForm);
        </script>

Controller: site.php

<?php
// Controller for mail form
return function($kirby, $pages, $page) {

    $alert = null;

    if($kirby->request()->is('POST') && get('submit')) {

// ... //

    }

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