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

Save visitors' ip with data saved by form

Started by Muut Archive 11 years ago · 6 replies · 817 views
11 years ago

Hello,

I'm using the form plugin to save email of the visitor. I also want to save the IP address off the visitor. How should I do that? I can use another twig template as a body, but I can't write PHP directly in the twig...

My form:

TWIG
form:
    name: subscribe
    action: /
    fields:
        - name: email
          label: Email
          placeholder: Enter your email address
          type: text
          vertical: false
          validate:
            rule: email
            required: true
    buttons:
        - type: submit
          value: Submit
    process: 
        - save:
            fileprefix: subscribe-
            dateformat: Ymd-His-u
            extension: txt
            body: "{% include 'forms/data.txt.twig' %}"
        - message: Thank you!
        - display: /thankyou

Thanks for your help

11 years ago

This is a question for @flaviocopes as he knows more about form plugin than me :)

11 years ago

Nobody? In the same spirit I would like to add the language of the user (it's a multilanguage website)..

10 years ago

Hi
I've got pretty much the same question. Regarding the IP address I tried adding this under the process heading and the IP shows on the confirmation page just fine

YAML
        -
            ip:
                label: User IP Address
-```

however when I try something similar with a field I get the label but no actual IP on the confirmation page or the email. Also I would have thought setting the field to "hidden" would also hide it from the confirmation page

YAML
    -
        name: User IP Address
        label: User IP Address
        type: hidden
        ip:

10 years ago

grav.uri.ip returns the IP of the user. So using it in the form with ip: "{{ grav.uri.ip}}" should do it.

10 years ago

This didn't output anything

YAML
-
            name: User IP Address
            label: User IP Address
            type: hidden
            ip: "{{ grav.uri.ip}}"

however adding it directly to the process section works perfectly

TWIG

body: '{% include ''forms/data.txt.twig'' %}User IP: {{ grav.uri.ip}}'
-```
Thanks
10 years ago

actually I realised I had my original setup wrong, obviously the ip collection needs to come before the send/save actions (duh) now that I've moved it to the first process action this line works perfectly.

YAML

-
            ip:
                label: User IP Address
---

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1362 9 years ago
Archive · by Muut Archive, 9 years ago
2 940 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2960 9 years ago
Archive · by Muut Archive, 9 years ago
3 1124 9 years ago