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

Variable to input form via link button

Started by lrnzt 9 years ago · 2 replies · 770 views
9 years ago

Can anyone help me with this?

I have a list of products, and in each has a button that goes to the form where it is possible to purchase the product

How can I do to automatically populate their name?
thanks

9 years ago

I would put your products into an array in the frontmatter. Try this:

TWIG
title: Products Page
process:
  twig: true
products:
  - title: Product A
    link: products/product-a
  - title: Product B
    link: products/product-b
---

# My Products

{% for product in page.header.products %}
<a class="button" href="{{ url(product.link) }}">{{ product.title }}</a> 
{% endfor %}

Something like this...

9 years ago

what I wanted was to pre-populate a field from a contact form with the product title

I tried this, but it did not work:
https://github.com/getgrav/grav/issues/826

now my code looks like this::

TWIG
<a href="{{ base_url }}/order-product#name={{ item.title }}">

var hashParams = window.location.hash.substr(1).split('&'); // substr(1) to remove the `#`
for(var i = 0; i < hashParams.length; i++){
var p = hashParams[i].split('=');
document.getElementById(p[0]).value = decodeURIComponent(p[1]);;
}

Does this work, should I leave it or do I have another more appropriate solution?

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 60 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 132 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