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

Dynamically add page url to contact form

Started by Muut Archive 10 years ago · 8 replies · 676 views
10 years ago

Hey guys,

I have set up a nice website which includes the same frontend form on each page. Is there a possibility to get the page (page url, title …) where the contact form is sent from? I thought of doing this with a hidden form field, but this lacks some documentation on the learn.grav page.

I want to find out which pages are generating the most leads

Could someone help me out with this?

10 years ago

You can use the hidden field and evaluate the default value:

YAML
        -
            name: page
            type: hidden
            default: grav.page.route
            evaluate: true
10 years ago

Thanks it seems to work. Just two more questions:

The hidden field is visible on the "thank you page" after the form is sent. Any chance to prevent this?

What does evaluate: true do?

10 years ago

HTTP headers include that data as a referer (yes the spec is misspelled), along with with some other fun data.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer

In PHP, you can access the header variables like $_SERVER['HTTP_REFERER'].

http://php.net/manual/en/reserved.variables.server.php

Note: the HTTP headers can be modified. Do not use this as a way to protect sensitive data. It is great for marketing insights as it will be accurate 99% of the time.

10 years ago

Alright. Already thought of doing this. Any hint on how to filter hidden input fields?
{% if not field.type == hidden %}does not work. :(

10 years ago

I think {% if not field.type == "hidden" %} will work. It's a string, not a variable.

10 years ago

Hmm thanks, but unfortunately it still does not work :/
Am I missing something else?

Code snippet from data.html.twig

TWIG

{% if not field.type == "hidden" %}
 <p class="sent-message__text">{{ string(form.value(field.name ?&quest; index))|nl2br }}</p>
{% endif %}
---
10 years ago

Got it… {% if field.type != "hidden" %}works!

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1349 9 years ago
Archive · by Muut Archive, 9 years ago
2 934 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2946 9 years ago
Archive · by Muut Archive, 9 years ago
3 1118 9 years ago