Hey everyone,
I am currently working on a website where a email form is embbed in the footer. So the email form is working great on every subpage but not on the mainpage. I use the MailHog to check if the emails where sended and every subpage send’s the data - only the mainpage doesn’t.
Did someone know if i have to treat the home.php different for something like this?
I’m using the GitHub - mzur/kirby-uniform: A versatile Kirby plugin to handle web form actions. plugin and my code looks like this:
controllers/site.php
<?php
use Uniform\Form;
return function ($page, $pages, $site, $kirby) {
$form = new Form([
'email' => [
'rules' => ['required', 'email'],
'message' => 'Email is required',
],
'message' => [],
]);
if ($kirby->request()->is('POST')) {
$form->emailAction([
'to' => 'me@example.com',
'from' => 'info@example.com',
]);
}
return compact(
'form',
);
snippets/contact.php
<form action="<?php echo $page->url() ?>" method="POST">
<input name="email" type="email" value="<?php echo $form->old('email'); ?>">
<textarea name="message"><?php echo $form->old('message'); ?></textarea>
<?php echo csrf_field(); ?>
<?php echo honeypot_field(); ?>
<input type="submit" value="Submit">
</form>
<?php if ($form->success()): ?>
Success!
<?php else: ?>
<?php snippet('uniform/errors', ['form' => $form]); ?>
<?php endif; ?>
home.php
...
<?php snippet('footer') ?>
snippets/footer.php
...
<?= snippet('contact') ?>
Something that I already found was, that $kirby->request()->is('POST')
is true on every subpage but not on the mainpage. And i think that could be the problem. But i don’t know how to fix it.
Does the controller work for the home page at all? Try putting a var_dump('whatever')
statement into your controller (before $kirby->request()->is('post')
). Do you get any output?
Yes. The site.php controller is used by the home.php-template if I var_dump()
i.e. kirby->request->is('post')
it shows bool(false)
on my website.
I also use a controller for the home.php this one looks like this:
controllers/home.php
<?php
return function ($page, $pages, $site, $kirby) {
$shared = $kirby->controller('site', compact('page', 'pages', 'site', 'kirby'));
return a::merge($shared , compact(
'someValues'
));
but in this controller i don’t use email form stuff at all.
niklas.garnholz:
return function ($page, $pages, $site, $kirby) {
$shared = $kirby->controller('site', compact('page', 'pages', 'site', 'kirby'));
return a::merge($shared , compact(
'someValues'
));
Well, theoretically, this controllers should be merged, but I’m not so sure this goes for conditions used in controllers. Never used shared controllers. I’d try copying the stuff into the home controller and see if it works or if it’s a different problem.
Thanks! Yeah, I’m kind of lost right now. Therefore that the issue is only on the mainpage and not on the subpages i’m kind of confused .
I don’t know if the config.php is interesting but i just put it here:
<?php
return [
'home' => 'startseite',
'languages' => true,
'languages.detect' => true,
'debug' => true,
'cache' => [
'pages' => [
'active' => true,
'ignore' => function() {
return kirby()->user() !== null;
}
]
],
'email' => [
'transport' => [
'type' => 'smtp',
'host' => 'localhost',
'port' => 1025,
'security' => false
]
]
];
I think caching is not a good idea if you use forms.
Do the other pages where it works also have their own controllers?
I will disable the cache and check if the problem still exist
And “No” the subpages are using the site.php-controller, they dont have custom ones.
Edit:
I disabled the cache but the problem is still there.
Then it’s probably an issue with the shared controller. Have you tested what I suggested and duplicated the form related code in the home.php controller?
Yes, I tried it. But still - the problem isn’t solved.
My home.php Controller looks something liek this rn:
<?php
use Uniform\Form;
return function ($page, $pages, $site, $kirby) {
$form = new Form([
'email' => [
'rules' => ['required', 'email'],
'message' => 'Email is required',
],
'message' => [],
]);
if ($kirby->request()->is('POST')) {
$form->emailAction([
'to' => 'me@example.com',
'from' => 'info@example.com',
]);
}
return compact(
'form',
);
Could there be a specific issue with POST Request on a mainpage? Is there an example for a form on a onepager? I guess this will help me a lot!
Hi, I am dropping into this topic, because I have the same issue.
The form works on every page except the home page. I have the controllers/site.php
form and tried with controllers/home.php
but no changes.
This seems like a Kirby issue. There is no error, nothing. Even form alerts are not triggered.
Ugh, found it
Could you try with a slash before #form.
<form action="<?php echo $page->url()?>/#form" method="post">
Another thing: When you send the form, does the send button get disabled? Or do you get a browser message when refreshing the page after clicking on the send button?
On the home page, it must have a slash before the #form
for the action.
<form action="<?php echo $page->url()?>/#form" method="post">
Solved
2 Likes