Uniform plugin - use submitted form data in success message

Hi,

I have a site with a form using Uniform to handle submission, validation etc. I now need to use some of the data from the form as part of the success message. Currently it looks like this:

<?php if($form->success()): ?>
  <p>Thank you for your enquiry</p>
<?php endif; ?>

I would like to do something like (this is pseudo-code to illustrate):

<?php if($form->success()): ?>
  <p>Thank you {{ name submitted in form }} for your enquiry</p>
<?php endif; ?>

Is this possible? I tried a dump of $form but can’t see the data there. If anyone has any thoughts that would be super.

You can always get all form data from the $_POST global

Thanks for the quick reply. I might be doing something wrong, but $_POST is an empty array when I do this:

<?php print_r($_POST) ?>

After submitting the form? Oh, wait, where is that code used? If it’s on a subsequent page, you need to store the data in the session and then later fetch it from the session.

It’s on the same page as the form - the implementation I have is pretty much the “Basic” setup from the docs: Basic - Kirby Uniform.

I wonder if Uniform is doing something with the data?

Actually I have this already:

return compact('form');

I could just add the data in there perhaps

Oh, ok, the $form->data($key) should do the job.

I’m not sure what I am doing wrong here. I have this now:

<?php if($form->success()): ?>
  <p>Thank you <?= $form->data('name') ?> for your enquiry</p>
<?php endif; ?>

But it is blank. If I dump $form->data() that is also empty.

My controller looks like this:

<?php

use Uniform\Form;

return function ($kirby,$page,$site,$pages)
{

    $shared = $kirby->controller('site' , compact('page', 'pages', 'site', 'kirby'));

    $form = new Form([
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'Please enter a valid email address',
        ],
        'name' => [
            'rules' => ['required'],
            'message' => 'Please enter your name',
        ],
        'phone' => [],
        'message' => [
            'rules' => ['required'],
            'message' => 'Please enter a message',
        ],
    ]);

    if ($kirby->request()->is('POST')) {
        if (get('formid') === 'enquiry') {
        $form->honeypotGuard()->honeytimeGuard([
            'key' => c::get('uniform.honeytime.key'),
            'seconds' => 3,
          ])->emailAction([
          'to' => 'info@website.com',
          'from' => 'no-reply@' . Url::host(),
          'subject' => 'New enquiry form submission on ' . $site->title(),
          'template' => 'simple-artwork',
          'data' => [
            'artwork' => $page->title() . " by " . $page->parent()->title()
          ]
        ])->done();
    }
    }

    return A::merge($shared,compact('form'));
};

How about $form->old($key)?

No that isn’t working either. Bit stumped here - @mzur do you have any ideas? I need to use the submitted data as part of the success response

Ok, I installed the plugin for testing, and the reason why the data is no longer available is because of the done() method attached to the email action. If you remove that, $form->data() will work. But that is not recommended, or you would need a manual redirect afterwards. In both cases, you would have to save the data to the session to be able to use it again in the template, because done() clears everything to prevent form resubmission.

It should work if you use withoutRedirect(), check if the form was successful after done(), then flash the data to the session and redirect manually.

Thanks for the answer @mzur that’s great. I will have a go at working out how to implement that - if you have time to help with some example code that would be awesome but I am going to crack on and see what I can do.

Hi both,

I have made a change, and it seems to be working, but I am not sure if there is an issue with what I have done. I have added the withoutRedirect() before done(), and not done anything else. It looks like this:

<?php

use Uniform\Form;

return function ($kirby,$page,$site,$pages)
{

    $shared = $kirby->controller('site' , compact('page', 'pages', 'site', 'kirby'));

    $form = new Form([
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'Please enter a valid email address',
        ],
        'name' => [
            'rules' => ['required'],
            'message' => 'Please enter your name',
        ],
        'phone' => [],
        'message' => [
            'rules' => ['required'],
            'message' => 'Please enter a message',
        ],
    ]);

    if ($kirby->request()->is('POST')) {
        if (get('formid') === 'enquiry') {
        $form->honeypotGuard()->honeytimeGuard([
            'key' => c::get('uniform.honeytime.key'),
            'seconds' => 3,
          ])->emailAction([
          'to' => 'info@website.com',
          'from' => 'no-reply@' . Url::host(),
          'subject' => 'New enquiry form submission on ' . $site->title(),
          'template' => 'simple-artwork',
          'data' => [
            'artwork' => $page->title() . " by " . $page->parent()->title()
          ]
        ])->withoutRedirect()->done();
    }
    }

    return A::merge($shared,compact('form'));
};

The email is sending, and this:

<?php if($form->success()): ?>
  <p>Thank you <?= $form->data('name') ?> for your enquiry</p>
<?php endif; ?>

Is now working. Validation also works as expected. But I haven’t done anything with sessions or redirects.

I guess what I am asking is can I call this done, or have I created a problem (with validation / security) that I am missing?

This disables the post/redirect/get pattern that Uniform uses. Users might submit the form again if they reload the page. I recommend that you still do the manual flashing (on success) and redirect. You can do this with Kirby Flash which is installed with Uniform anyway.

Thanks @mzur for the clarification.

I have made some changes to my controller, but am now hitting an issue where my success conditional as part of the page the form is on is now not working. This is my updated controller, using the withoutRedirect and also flashing:

<?php

use Uniform\Form;

return function ($kirby,$page,$site,$pages)
{

    $shared = $kirby->controller('site' , compact('page', 'pages', 'site', 'kirby'));

    $form = new Form([
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'Please enter a valid email address',
        ],
        'name' => [
            'rules' => ['required'],
            'message' => 'Please enter your name',
        ],
        'phone' => [],
        'message' => [
            'rules' => ['required'],
            'message' => 'Please enter a message',
        ],
    ]);

    if ($kirby->request()->is('POST')) {
        if (get('formid') === 'enquiry') {
        $form->honeypotGuard()->honeytimeGuard([
            'key' => c::get('uniform.honeytime.key'),
            'seconds' => 3,
          ])->withoutRedirect()->emailAction([
          'to' => 'info@website.com',
          'from' => 'no-reply@' . Url::host(),
          'subject' => 'New enquiry form submission on ' . $site->title(),
          'template' => 'simple-artwork',
          'data' => [
            'artwork' => $page->title() . " by " . $page->parent()->title()
          ]
        ]);
        }

        if($form->success()) {
            flash('name', $form->data('name'));
            go($page->url());
        }
    }

    return A::merge($shared,compact('form'));
};

With this I am getting validation as expected, and also the email is sending. However after the redirect my following conditional statement isn’t working:

<?php if($form->success()): ?>
  <p>Thank you <?= flash('name') ?> for your enquiry</p>
<?php endif; ?>

I guess this is to do with the redirect? I really appreciate your help so far, could you advise what I need to do here to get this to work?