Save visitors' ip with data saved by form


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:

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

Thanks for your help

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

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

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

                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

        name: User IP Address
        label: User IP Address
        type: hidden

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

This didn’t output anything

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

however adding it directly to the process section works perfectly

body: '{% include ''forms/data.txt.twig'' %}User IP: {{ grav.uri.ip}}'

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.

                label: User IP Address