<p>Twig (at least within the context of Craft) doesn't work this way I'm afraid. The Twig template is processed on the server and HTML is returned to the browser.</p>

<p>You're trying to change the Twig based on an event in the Browser, but that at that time, there is no longer Twig, just HTML.</p>

<p>So your solution will need to involve Javascript to change the HTML/DOM rather than the approach you're going for here.</p>

CC BY-SA 4.0
2019-07-29T12:47:11.797Z
Thanks! I'am solving it right now with js but I was hoping there will be a "simpler" solution.
ves
CC BY-SA 4.0
2019-07-29T12:41:44.227Z
2019-07-29T12:41:44.227Z
foamcow
<p>You can do it without JavaScript (if you don't mind the page refreshing) by accessing the request variable:</p>

<p><a href="https://docs.craftcms.com/api/v3/craft-web-twig-variables-request.html#public-methods" rel="noreferrer">https://docs.craftcms.com/api/v3/craft-web-twig-variables-request.html#public-methods</a></p>

<pre><code>{{ craft.app.request.getParam('foo') }} {# Should be the value of the input named 'foo' #}
</code></pre>

CC BY-SA 4.0
2019-07-29T15:04:07.143Z
This is exactly what I ended up using. Thanks anyways! The only thing that bothers me is that I only managed to get it work with a form with a "get" method and the parameters are visible in the address bar. Do you know a way to hide them?
ves
CC BY-SA 4.0
2019-07-29T18:38:37.573Z
You can set the form's method to 'post' and the server should still be able to access the request variables, without showing them in the address bar.
missmatsuko
CC BY-SA 4.0
2019-07-29T14:43:39.400Z
2019-07-29T14:43:39.400Z
missmatsuko
<p>I am new to Craft and Twig in general. I have encountered the following problems and struggling to find a solution for hours.</p>

<ol>
<li>I want to access the value of "foo" input field inside the twig delimiters to set the "json" variable.  </li>
<li>I want to change the "clicked" boolean, whenever the submit button is clicked.</li>
</ol>

<p>The myFunc is a function that I am using from a custom plugin so it is not relative. 
I have commented what I've last tried and does not work for me.</p>

<p>I am using Craft 3, if that helps.</p>

<pre><code>{% block body %}
    {% set clicked = false %}

    &lt;form name="search" action="{% set clicked = true %}"&gt;
        &lt;label for="foo"&gt;Foo&lt;/label&gt;
        &lt;input name="foo" type="text"/&gt;
        &lt;input type="submit" value="Get Json"/&gt;
        {#    &lt;a href="{{ set clicked = true }}"#}
    &lt;/form&gt;

    {% if clicked %}
        {% set json = jsonator_myFunc(search.foo.vars.value) %}
    {% endif %}

{% endblock %}
</code></pre>

<p><strong>EDIT/SOLUTION:</strong></p>

<p>A few more days into Craft CMS and chill and I realize how silly my question is. </p>

<p>Thank you for the replies. The solution which I was looking for is:</p>

<pre><code>craft.request.getParam('foo')
</code></pre>

CC BY-SA 4.0
2019-07-29T15:15:03.310Z
For clarity, Twig is a wrapper for PHP, a _server-side_ language. Twig/PHP gets parsed down to regular HTML before it gets sent to the browser. Essentially, you'd interact with Twig in the exact same way as you'd interact with PHP.
Lindsey D
<templating><craft3><php><yii><twigextension>
How to access HTML input values from Twig and vice versa
# How to access HTML input values from Twig and vice versa

(Asked by: username_0 on 2019-07-29)

I am new to Craft and Twig in general. I have encountered the following problems and struggling to find a solution for hours.

I want to access the value of "foo" input field inside the twig delimiters to set the "json" variable.  
I want to change the "clicked" boolean, whenever the submit button is clicked.

The myFunc is a function that I am using from a custom plugin so it is not relative. 
I have commented what I've last tried and does not work for me.
I am using Craft 3, if that helps.
{% block body %}
    {% set clicked = false %}

    
        Foo
        
        
        {#    

    {% if clicked %}
        {% set json = jsonator_myFunc(search.foo.vars.value) %}
    {% endif %}

{% endblock %}

EDIT/SOLUTION:
A few more days into Craft CMS and chill and I realize how silly my question is. 
Thank you for the replies. The solution which I was looking for is:
craft.request.getParam('foo')

**Tags:** templating, craft3, php, yii, twigextension

> **Comments:**
> **username_1**: For clarity, Twig is a wrapper for PHP, a _server-side_ language. Twig/PHP gets parsed down to regular HTML before it gets sent to the browser. Essentially, you'd interact with Twig in the exact same way as you'd interact with PHP.  

---

## 2 answers

### Answer by username_3 on 2019-07-29. Score: 5 | Accepted answer

You can do it without JavaScript (if you don't mind the page refreshing) by accessing the request variable:
https://docs.craftcms.com/api/v3/craft-web-twig-variables-request.html#public-methods
{{ craft.app.request.getParam('foo') }} {# Should be the value of the input named 'foo' #}

> **Comments:**
> **username_0**: This is exactly what I ended up using. Thanks anyways! The only thing that bothers me is that I only managed to get it work with a form with a "get" method and the parameters are visible in the address bar. Do you know a way to hide them?  
> **username_3**: You can set the form's method to 'post' and the server should still be able to access the request variables, without showing them in the address bar.  

---

### Answer by username_2 on 2019-07-29. Score: 3

Twig (at least within the context of Craft) doesn't work this way I'm afraid. The Twig template is processed on the server and HTML is returned to the browser.

You're trying to change the Twig based on an event in the Browser, but that at that time, there is no longer Twig, just HTML.

So your solution will need to involve Javascript to change the HTML/DOM rather than the approach you're going for here.

> **Comments:**
> **username_0**: Thanks! I'am solving it right now with js but I was hoping there will be a "simpler" solution.  

---

