Uniform Plugin - store form entry in DB

Apologies if this has been asked before, the forum search gave me nothing that obviously answers this.

I would like to store the form submission in a database. How would i do this? I know uniform can store to a log file out the box, but I would like a DB store ideally. How i do this? My form only has two fields - name and email.

Code examples welcomed and appreciated :slight_smile:

Thanks

Wow. Surprised nobody can answer this… i cant be the only one trying to do this.

In principle, you need a new action, e.g. a StoreAction in combination with using Kirby’s database class. The custom action would contain the code to persist your data in a database (instead of logging it to file or sending an email).

Ok i gave this a shot but i get a whoops…

Connection string is in my config.php file

Class 'Uniform\Actions\db' not found

Heres my action (i know it works because the var_dump works):

<?php

namespace Uniform\Actions;

class StoreAction extends Action
{
    public function perform()
    {
        try {

            // var_dump($this->form->data());

            db::insert('contact', array(
              'name'    => $this->form->data()->name(),
              'email'   => $this->form->data()->email(),
              'message' => $this->form->data()->message(),
              'date'    => 'NOW()',
            ));


        } catch (\Exception $e) {
            $this->fail($e->getMessage());
        }
    }
}

What am i missing?

PHP tries to find the database class in the Uniform\Actions namespace. You have to use the database class before you can use it.

Example: Get plugin config values without providing prefix or fallback

Thanks…

So i added…

<?php
namespace Uniform\Actions;
use db;
...

Now i get a new whoops…

Call to a member function name() on array

sigh.

You can’t use methods with an array, only with objects. Use the index to get an array value:

$this->form->data()['name']

Or rather:

$data = $this->form->data();
db::insert('contact', array(
              'name'    => $data['name'],
             
            ));

This worked, thank you.

$this->form->data()['name']

I really ought to get back to watching those ‘how to PHP’ videos… :slight_smile:

Yep, that would be useful, because these things are quite basic… On the other hand, if you have made the same mistake a couple of times, you will stop making them in the end :wink:

Cookies make me fat, but it doesnt stop me eating cookies.

If it helps someone else, heres my full uniform-actions.php

<?php

namespace Uniform\Actions;
use db;

class StoreAction extends Action
{
    public function perform()
    {
        try {
            // var_dump($this->form->data());
            $data = $this->form->data();
            db::insert('contact', array(
              'name'    => $data['name'],
              'email'   => $data['email'],
              'message' => $data['message'],
              'date'    => 'NOW()',
            ));
        } catch (\Exception $e) {
            $this->fail($e->getMessage());
        }
    }
}

And the controller…

<?php

use Uniform\Form;

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

    $form = new Form([

        'name' => [
            'rules' => ['required'],
            'message' => 'Please enter your name',
        ],
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'Please enter a valid email address',
        ],
        'message' => [
            'rules' => ['required'],
            'message' => 'Pease make some comments if you have any!',
        ],
    ]);

    if (r::is('POST')) {
        $form->emailAction([
          'to'      => 'you@example.com',
          'from'  => 'noreply@example.com',
          'subject' => $site->title()->html() . ' - contact form'
        ])->StoreAction();
    }



    return compact('form');
};
1 Like

Also, I’d store form data in a variable first and then get the different keys, instead of repeating $this->form->data() several times.

In that case you should probably fix this page in the docs then.

And actually got this inserted into the DB, not NOW() as a string

2017-11-08 22:06:53

Oh, ok, I’m mistaken, sorry, seems to be a MySQL function, not PHP. Removed my comment.

No worries, thanks for the help.