Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

Form field set by URL parameter

Solved by Andy Miller View solution

Started by Robgnu 9 years ago · 3 replies · 2541 views
9 years ago

Hello everyone,

I'm searching for a solution and hopefully someone can help or advise me. I have a simple contact form in my website wich is working very well. As a service for my customers, I created a QR-Code on a piece of paper with a personalized link to the contact-form. For example https://example.com/contact?name=Example&value1=value1.
Now I would like to prefill the form with the given GET params.

All I have found was a closed but not solved issue on Github.

Can anybody give me an advise how I can solve that problem?

Thank you.
Robgnu

9 years ago Solution

Ok, this can be done pretty easily but requires you create a new static method that effectively wraps the Uri->query() method:

In your theme's PHP (or in a custom plugin if you have one handy) add a new static function :

PHP
<?php
namespace Grav\Theme;

use Grav\Common\Theme;
use Grav\Common\Grav;

class Antimatter extends Theme
{
    public static function getQueryVar($var)
    {
        return Grav:: instance()['uri']->query($var);
    }
}

Then in your form definition YAML put something like:

YAML
      email:
            label: Email
            placeholder: 'Enter your email address'
            data-default@: ['\Grav\Theme\Antimatter::getQueryVar', 'email']
            type: text
            validate:
                rule: email
                required: true

Then when you go to your page with the form simply append [email protected] to test with (really it should be URLEncoded - ?email=foo%40bar.com) and your form should get the default value added. You can reuse this for any field you like.

👍 2
9 years ago

Hello rhuk,

thank you very much. I would not have guessed.
My first try works great. I added an URL parameter and the form was filled by the given value - great. But from that point on every page refresh doesn't change the default value: The field was filled with the value I entered first. It makes no difference whether I give no parameter or another value. When I call the form with another Browser it still shows the first value.
I set the cache_enable: falseoption, but it makes no difference. Any idea?

Robert.

last edited 11/29/17 by Robgnu
8 years ago

I noticed when trying this solution that file upload breaks when the URL contains parameters (no file will be saved to user/data nor attached to email, value logged as null).

example url I used /jobs/form:show?value=something.

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1135 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 61 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 134 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 108 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 127 7 months ago