Sending the current page with Uniform

Hi there and @mzur,

I’ve setup a contact form with Uniform. The form is a snippet that I’m re-using in a template. The template is used to render different pages, like /financial-translation-services or professional-translation-services.

The code:

<?php
use Uniform\Form;

$form = new Form([
  'email' => [
    'rules' => ['required', 'email'],
    'message' => 'Please enter a valid email address.',
  ],
  'source' => [],
  'target' => [],
  'words' => [],
  'page' => [],
]);

if (r::is('POST')) {
  $form->emailAction([
      'to' => 'mail@server.com',
      'from' => 'bot@server.com',
      'subject' => 'New opp from {email}',
  ]);
}
?>

I would like the form to send the page where this template was used, so we can learn which pages are converting the most. So, I’m using this in the body of the template:

<?php $form->data($key = 'page', $value = $page->uid()); ?>

By doing print_r($form->data()); it shows the value of the page key. However, when I receive the form, the page key is not sent. I get all the other values, except this one.

The full code is here: https://gist.github.com/rodrigogalindez/9a32e3d3dc0aab8ae5d099f0a7862887

What could be happening here? Thanks.

What about the snippet that contains the stuff that is sent?

You mean the Uniform template snippet? I’m not using one, just the default from Uniform. Based on the docs, Uniform should send all keys if there’s no template to be used…

Did you try setting the page UID as the value of a hidden field? I think it will just pick that up on submission, as long as it matches up with controller.

How do I do that? My PHP knowledge is close to zero.

As far as I can see, you try to set the page as key/value pair in your template, but that will not be part of the data when the form is sent, I think.

A. hidden field would be an option.

Just add an additional field and add the attribute.hidden.

Can you please paste some code?

Something like…

<input id="page" name="page" type="hidden" value="<?= $page->uid()?>" >
1 Like

Thanks! It works :slight_smile:

1 Like