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?
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
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:
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=foo@bar.com 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.
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?
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.