Fill Uniform field based on data from previous page

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 :relaxed:

Well, if you come from the menu, there is no $_POST variable and therefor the index product will not exist. So if you stick to this method, you would have to use an if-statement. However, I’d use a link with a parameter instead of a hidden form. Then you can check if the parameter exists like this:

if(param('product')) {
  $product = param('product');
}

This will likely also prevent your CSRF token issue…

Addionally, you have to add the subject to the email action, see the documentation of the plugin for details.

Thank you for the tip! Have been looking into links with parameters now, but not sure I fully understand.

<!-- Product Page -->
<a href="<?= page('enquiries')->url() . '?product=' . $page->title()->html() ?>">Enquire</a>

For checking the parameter, I’m putting the code you posted into the beginning of the Enquiries controller, but getting a “syntax error, unexpected ‘;’” for that line?

One of my favorites, missing parenthesis… :blush:

Eagle eyes. Sneaky little parenthesis!

Works a treat when coming from a product page, the ‘Subject’ box is filled with the title:

<!-- Enquiries Page -->
<input name="subject" type="text" value="I'd like to enquire about <?php echo $_GET['product'] ?>" placeholder="Subject">

But now do I need to do an if/else to have it the field be empty when coming from the menu link? Before adding the product, the subject was:

value="<?php if echo $form->old('subject'); ?>"

Yes, you would need an if statement…

value="<?php if (isset($_GET['product'])) { echo ("I'd like to enquire about " . $_GET['product']); } else { echo $form->old('subject'); } ?>"

Got it working, thank you for your help @texnixe :smile: