Hello!
I have an Enquiries page with a Uniform contact form, which works great. The Enquiries page can be reached from a link in the menu - which is accessible on every page - but also from a separate button on individual product pages.
I am trying to make it so that if the Enquiries page is reached from the menu, the ‘Subject’ field of the form is blank, but if the Enquiries page is reached using the button from a product page, the ‘Subject’ field is pre-filled with something like ‘I would like to enquire about Product X’, with ‘Product X’ being the Title of the product page that the Enquries page was reached from.
I thought of using a hidden form on the Product page to send the title to the Enquiries page:
<!-- Product Page -->
<form action="<?= page('enquiries')->url() ?>" method="POST">
<input name="product" type="text" value="<?= $page->title()->html() ?>">
<input class="btn" type="submit" value="Send">
</form>
and
<!--Enquiries Page Controller-->
<?php
use Uniform\Form;
return function ($site, $pages, $page) {
$product = htmlspecialchars($_POST['product']);
$form = new Form([
'name' => [
'rules' => ['required'],
'message' => 'Name',
],
'email' => [
'rules' => ['required', 'email'],
'message' => 'E-mail',
],
'phone' => [
'rules' => ['required', 'num'],
'message' => 'Phone number',
],
'subject' => [
'rules' => ['required', 'text'],
'message' => 'Subject',
],
'message' => ['Message'],
]);
if (r::is('POST')) {
$form->emailAction([
'to' => 'name@email.com',
'from' => 'nom@email.com',
]);
}
return compact('form', 'product');
};
But am not sure how to get the POST data from this page to work with the Uniform form… definitely doing something wrong as I get this error when using the Product page submit button:
The CSRF token was invalid.
And this error if I try to get to the Enquiries page from the menu:
Undefined index: product
Am (clearly!) new to using form data in Kirby, so any help or pointers would be appreciated