<h3>Twig</h3>
<p>You can check for certain URL parameters via</p>
<pre><code>{% set param = craft.app.getRequest().getParam('id') %}
{% if param is not null %}
    {% set entry = craft.app.getElements().getElementById(param) %}
    {# insert your new field value! not the label, the value/handle
    {% do entry.setFieldValue('fieldHandle', 'Reserved') %}
    {% do craft.app.getElements().saveElement(entry) %}
{% endif %}
</code></pre>
<p>However I would not recommend doing this in Twig</p>
<h3>PHP</h3>
<p>You can see how to use <a href="https://docs.craftcms.com/v2/plugins/variables.html#app" rel="nofollow noreferrer">Template Variables</a> and create your boilerplate at <a href="http://pluginfactory.io" rel="nofollow noreferrer">pluginfactory.io</a></p>
<pre><code>{% do craft.myAwesomePlugin.saveEntry(entry, {fieldHandle: 'yourValue'}) %}
</code></pre>
<p>And your function could be</p>
<pre><code>public function saveEntry($entry, $attributes = []){
    $entry-&gt;getContent()-&gt;setAttributes($attributes);
    craft()-&gt;entries-&gt;saveEntry($entry);
}
</code></pre>
<p>That's just a &quot;quick&quot; version, I suggest you to take a look at <code>EntriesController</code> to see how Craft handles it in detail</p>

CC BY-SA 4.0
2018-06-26T07:11:11.500Z
Thanks! I can see by the "craft.app.xxx" that this is Craft 3 code? The project still runs on Craft 2, is this also available in 2?
Why wouldn't you recommend doing this in Twig?
mdmngz
CC BY-SA 4.0
2018-06-26T07:14:55.917Z
No it does not work in Craft 2 - Twig is a template engine and not supposed to contain to much logic. You only have limited opportunities for error handling so if something unexpected happens you can't handle it really well
Robin Schambach
CC BY-SA 4.0
2018-06-26T07:30:53.697Z
Yes, I understand, you asked if I wanted to use Twig or PHP so I thought I had a choice and would avoid PHP if possible :) Can you set me on the way on how to do this with Craft 2? I assume this needs a custom plugin? I'm not afraid to write some PHP code, but just need to know some steps in order to get this done
mdmngz
CC BY-SA 4.0
2018-06-26T07:41:45.427Z
I've edited the answer
Robin Schambach
CC BY-SA 4.0
2018-06-26T07:03:23.013Z
2018-06-26T07:41:37.250Z
2020-06-17T08:56:27.903Z
Robin Schambach
<p>So I'm having this entry (a product) that contains a selectbox field with two values:</p>

<ul>
<li>Reserved</li>
<li>Available</li>
</ul>

<p>By default this field is set to 'Available', but when the user lands on a certain page (a thank you page after "ordering" this product with the entry id as a querystring in the URL), I'd like to change the value of that selectbox to 'Reserved'.</p>

<p>Anyone can put me in the right direction on this one?</p>

<p>Thanks in advance!</p>

CC BY-SA 4.0
2018-06-25T20:14:07.150Z
Do you want to resave the entry or just want to display certain things based on url parameter? Do you want it to save automatically or do you want to create a form?
Robin Schambach
CC BY-SA 4.0
2018-06-25T21:00:09.457Z
Resaving the entry automatically at page load of the "/thank-you?id=33" page (with 33 being the entry id of that product)
mdmngz
CC BY-SA 4.0
2018-06-25T21:00:58.300Z
Do you want to use twig or would you use PHP for it?
Robin Schambach
CC BY-SA 4.0
2018-06-26T06:59:32.833Z
Twig, please! If PHP can be avoided, the better :)
mdmngz
<frontend><saveentry>
How do I change the value of a select box field of a certain entry through the front-end?
# How do I change the value of a select box field of a certain entry through the front-end?

(Asked by: username_0 on 2018-06-25)

So I'm having this entry (a product) that contains a selectbox field with two values:

* Reserved
* Available

By default this field is set to 'Available', but when the user lands on a certain page (a thank you page after "ordering" this product with the entry id as a querystring in the URL), I'd like to change the value of that selectbox to 'Reserved'.

Anyone can put me in the right direction on this one?

Thanks in advance!

**Tags:** frontend, saveentry

> **Comments:**
> **username_1**: Do you want to resave the entry or just want to display certain things based on url parameter? Do you want it to save automatically or do you want to create a form?  
> **username_0**: Resaving the entry automatically at page load of the "/thank-you?id=33" page (with 33 being the entry id of that product)  
> **username_1**: Do you want to use twig or would you use PHP for it?  
> **username_0**: Twig, please! If PHP can be avoided, the better :)  

---

## 1 answers

### Answer by username_1 on 2018-06-26. Score: 1

Twig
You can check for certain URL parameters via
{% set param = craft.app.getRequest().getParam('id') %}
{% if param is not null %}
    {% set entry = craft.app.getElements().getElementById(param) %}
    {# insert your new field value! not the label, the value/handle
    {% do entry.setFieldValue('fieldHandle', 'Reserved') %}
    {% do craft.app.getElements().saveElement(entry) %}
{% endif %}

However I would not recommend doing this in Twig
PHP
You can see how to use Template Variables and create your boilerplate at pluginfactory.io
{% do craft.myAwesomePlugin.saveEntry(entry, {fieldHandle: 'yourValue'}) %}

And your function could be
public function saveEntry($entry, $attributes = []){
    $entry->getContent()->setAttributes($attributes);
    craft()->entries->saveEntry($entry);
}

That's just a "quick" version, I suggest you to take a look at EntriesController to see how Craft handles it in detail

> **Comments:**
> **username_0**: Thanks! I can see by the "craft.app.xxx" that this is Craft 3 code? The project still runs on Craft 2, is this also available in 2? Why wouldn't you recommend doing this in Twig?  
> **username_1**: No it does not work in Craft 2 - Twig is a template engine and not supposed to contain to much logic. You only have limited opportunities for error handling so if something unexpected happens you can't handle it really well  
> **username_0**: Yes, I understand, you asked if I wanted to use Twig or PHP so I thought I had a choice and would avoid PHP if possible :) Can you set me on the way on how to do this with Craft 2? I assume this needs a custom plugin? I'm not afraid to write some PHP code, but just need to know some steps in order to get this done  
> **username_1**: I've edited the answer  

---

