<p>To be able to do this you need to create a custom module in your Craft.
Usually when you install craft, craft already include an example of Module inside the modules folder, you create you own but let use this first for example.</p>
<p>Now let's check the config/app.php, and you will see something like this :</p>
<pre><code>'modules' =&gt; [
    'my-module' =&gt; \modules\Module::class,
],
</code></pre>
<p>This means you module is registered and the module id is <code>my-module</code>. So the next step is you need controller to call your code, then go to <code>modules</code> folder create a folder called <code>controllers</code>, then inside it create a class something like this TestController, to be note the class name must use suffix <code>Controller</code>. So inside your TestController should be like this :</p>
<pre><code>class TestController extends Controller
{
  public function actionTest() {

   }
}
</code></pre>
<p>So in here we can put our code inside the actionTest function. To save element, You can do it by calling saveElement from Craft and pass the entry element, it should be like this :</p>
<pre><code>$entry = Entry::findOne($id);
$entry-&gt;setFieldValue('priceProduct', $price);
Craft::$app-&gt;elements-&gt;saveElement($entry);
</code></pre>
<p>so the full code can be like this :</p>
<pre><code>class TestController extends Controller
{
  public function actionTest() {
      $id = 2;
      $price = '100';
      $entry = Entry::findOne($id);
      $entry-&gt;setFieldValue('priceProduct', $price);
      Craft::$app-&gt;elements-&gt;saveElement($entry);
   }
}
</code></pre>
<p>Then lastly you need to register the route for your controller, so go to config/routes.php put something like this :</p>
<pre><code>return [
    'test' =&gt; 'my-module/test/test',
];
</code></pre>
<p>The explanation for the path : my-module is our module id that we register inside the app.php, then first test segment based on the class name so our controller class is TestController, and the last test segment is based on function name (actionTest).</p>
<p>After that try to call it by <code>yousiteurl/test</code>.
For the full explanation how to create controller and module you can go here.
<a href="https://craftcms.com/docs/4.x/extend/controllers.html#request-validation-methods" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/extend/controllers.html#request-validation-methods</a></p>

CC BY-SA 4.0
2022-09-09T17:05:28.607Z
I tried to paste this code into a file price.php but nothing happened. Why is that? Don't judge strictly, I'm new to CRAFT CMS
Dimi
CC BY-SA 4.0
2022-09-11T00:50:55.483Z
@Dimi, can you show us your section field configuration. There probably an error on validation. The return of craft save element is true or false, if you want to check if the save is success or not.
aodihis
CC BY-SA 4.0
2022-10-11T20:12:40.573Z
The field I am trying to update has the type "Plain Text Fields". Nothing supernatural. Maybe it won't be possible to launch and execute it just like that in Craft CMS? Maybe it is necessary to create a module or plugin for these purposes?
Dimi
CC BY-SA 4.0
2022-10-11T20:54:00.733Z
I forgot to clarify. I use Craft CMS 4.2.5.2
Dimi
CC BY-SA 4.0
2022-10-11T20:57:41.220Z
Maybe we should use something like this at the beginning of the file price.php :
usa Craft;
use craft\elements\Entry;
Dimi
CC BY-SA 4.0
2022-10-11T21:54:50.160Z
Forgive me for my possibly stupid questions. I'm new to this CMS
Dimi
CC BY-SA 4.0
2022-10-13T06:01:06.297Z
@Dimi, yes you need to create a module for these purpose, you cannot call the php directly on Craft. So you need to create module and put a controller inside them to run that script.
aodihis
CC BY-SA 4.0
2022-10-13T22:40:07.013Z
Could you provide some easy example on creating a module in craft CMS. Some very simple example. I just can't imagine how to run the code I need on a separate url in this content management system. It will be very useful for me) I really want to learn how to work in this system
Dimi
CC BY-SA 4.0
2022-10-14T03:58:05.843Z
@Dimi, sure I edited the answer.
aodihis
CC BY-SA 4.0
2022-10-16T22:33:23.893Z
Detailed answer) But the first time, of course, nothing started. Here's what I did:
1. Went to the modules folder
2. Created the controllers folder there
3. In the controllers folder, I created a file TestController.php and put the controller class in this file
4. In the file config/routes.php I have entered 'test' => 'my-module/test/test

When trying to go to the right URL(https://steamcool.site/test ) I get HTTP error 403 – Forbidden – yii\web\Forbidden HttpException

P.S. I've simplified actionTest() a bit by putting just the code in it so far: echo "hello, world";
Dimi
CC BY-SA 4.0
2022-10-16T23:08:45.107Z
Very interesting. The 403 error is an access-related error, as I understand it. After thinking about it, I logged in to the administrative panel of the site. And a miracle, I saw my line "hello, world". However, I also got some more incomprehensible lines. Is there any way to make my test url available not only to the site administrator?
Dimi
CC BY-SA 4.0
2022-10-17T00:15:26.297Z
I found an error that caused me to output incomprehensible lines. Instead of echo, you should use return. It remains to be decided so that the code is visible not only to the admin
Dimi
CC BY-SA 4.0
2022-10-18T03:37:52.767Z
Yes, you need to add this line public int|bool|array $allowAnonymous = true; because by default Craft Controller is set to false.
aodihis
CC BY-SA 4.0
2022-10-18T22:47:30.613Z
super! It works))) A very valuable lesson. I understand that in order to better navigate this, you would need to know Yii? Yes?
And also, for the sake of experiment, I drove in return Entry::findOne($id). The title of the element was displayed on the screen. And how can I output the additional field of this element that I created?
Dimi
CC BY-SA 4.0
2022-10-19T01:22:16.250Z
Yes you can get entry by this $entry = Entry::findOne($id), and then if you want to get additional field just return $entry->additionalField
aodihis
CC BY-SA 4.0
2022-10-20T20:20:37.063Z
And I used this construction: $price = Entry::findOne(25)->GetFieldValue('priceProduct'). In principle, it is clear how to find out the values of the fields of a certain element. Please tell me, what kind of construction should I use to sort through all the elements of a certain section?
Dimi
CC BY-SA 4.0
2022-10-21T03:50:36.987Z
Oh If you want to find element on certain section you can do like this $entries = Entry::find()->section('sectionName')->all() you can put the orderBy if you want to do some sorting example like this Entry::find()->section('sectionName')->orderBy('title')all()
aodihis
CC BY-SA 4.0
2022-09-06T05:47:22.253Z
2022-10-14T03:57:39.450Z
2022-10-14T03:57:39.450Z
aodihis
<p>please help me find a solution. Or at least tell me where to read about it.</p>
<p>Task: There is a PHP file price.php . It has one variable, for example $price = 100;
I created the &quot;store&quot; section. I need that when you click on my site on the link <a href="https://mysite.com/price.php" rel="nofollow noreferrer">https://mysite.com/price.php</a> the value of the $price variable was added to the element with id = 2 in the priceProduct field I created. Such a simple field update. How to implement this in CRAFT CMS?</p>

<craft3>
How to update a field?
# How to update a field?

(Asked by: username_0 on 2022-09-05)

please help me find a solution. Or at least tell me where to read about it.

Task: There is a PHP file price.php . It has one variable, for example $price = 100;
I created the "store" section. I need that when you click on my site on the link <https://mysite.com/price.php> the value of the $price variable was added to the element with id = 2 in the priceProduct field I created. Such a simple field update. How to implement this in CRAFT CMS?

**Tags:** craft3

---

## 1 answers

### Answer by username_1 on 2022-09-06. Score: 2 | Accepted answer

To be able to do this you need to create a custom module in your Craft.
Usually when you install craft, craft already include an example of Module inside the modules folder, you create you own but let use this first for example.
Now let's check the config/app.php, and you will see something like this :
'modules' => [
    'my-module' => \modules\Module::class,
],

This means you module is registered and the module id is my-module. So the next step is you need controller to call your code, then go to modules folder create a folder called controllers, then inside it create a class something like this TestController, to be note the class name must use suffix Controller. So inside your TestController should be like this :
class TestController extends Controller
{
  public function actionTest() {

   }
}

So in here we can put our code inside the actionTest function. To save element, You can do it by calling saveElement from Craft and pass the entry element, it should be like this :
$entry = Entry::findOne($id);
$entry->setFieldValue('priceProduct', $price);
Craft::$app->elements->saveElement($entry);

so the full code can be like this :
class TestController extends Controller
{
  public function actionTest() {
      $id = 2;
      $price = '100';
      $entry = Entry::findOne($id);
      $entry->setFieldValue('priceProduct', $price);
      Craft::$app->elements->saveElement($entry);
   }
}

Then lastly you need to register the route for your controller, so go to config/routes.php put something like this :
return [
    'test' => 'my-module/test/test',
];

The explanation for the path : my-module is our module id that we register inside the app.php, then first test segment based on the class name so our controller class is TestController, and the last test segment is based on function name (actionTest).
After that try to call it by yousiteurl/test.
For the full explanation how to create controller and module you can go here.
https://craftcms.com/docs/4.x/extend/controllers.html#request-validation-methods

> **Comments:**
> **username_0**: I tried to paste this code into a file price.php but nothing happened. Why is that? Don't judge strictly, I'm new to CRAFT CMS  
> **username_1**: @Dimi, can you show us your section field configuration. There probably an error on validation. The return of craft save element is true or false, if you want to check if the save is success or not.  
> **username_0**: The field I am trying to update has the type "Plain Text Fields". Nothing supernatural. Maybe it won't be possible to launch and execute it just like that in Craft CMS? Maybe it is necessary to create a module or plugin for these purposes?  
> **username_0**: I forgot to clarify. I use Craft CMS 4.2.5.2  
> **username_0**: Maybe we should use something like this at the beginning of the file price.php : usa Craft; use craft\elements\Entry;  
> **username_0**: Forgive me for my possibly stupid questions. I'm new to this CMS  
> **username_1**: @Dimi, yes you need to create a module for these purpose, you cannot call the php directly on Craft. So you need to create module and put a controller inside them to run that script.  
> **username_0**: Could you provide some easy example on creating a module in craft CMS. Some very simple example. I just can't imagine how to run the code I need on a separate url in this content management system. It will be very useful for me) I really want to learn how to work in this system  
> **username_1**: @Dimi, sure I edited the answer.  
> **username_0**: Detailed answer) But the first time, of course, nothing started. Here's what I did: 1. Went to the modules folder 2. Created the controllers folder there 3. In the controllers folder, I created a file TestController.php and put the controller class in this file 4. In the file config/routes.php I have entered 'test' => 'my-module/test/test  When trying to go to the right URL(https://steamcool.site/test ) I get HTTP error 403 – Forbidden – yii\web\Forbidden HttpException  P.S. I've simplified actionTest() a bit by putting just the code in it so far: echo "hello, world";  
> **username_0**: Very interesting. The 403 error is an access-related error, as I understand it. After thinking about it, I logged in to the administrative panel of the site. And a miracle, I saw my line "hello, world". However, I also got some more incomprehensible lines. Is there any way to make my test url available not only to the site administrator?  
> **username_0**: I found an error that caused me to output incomprehensible lines. Instead of echo, you should use return. It remains to be decided so that the code is visible not only to the admin  
> **username_1**: Yes, you need to add this line public int|bool|array $allowAnonymous = true; because by default Craft Controller is set to false.  
> **username_0**: super! It works))) A very valuable lesson. I understand that in order to better navigate this, you would need to know Yii? Yes? And also, for the sake of experiment, I drove in return Entry::findOne($id). The title of the element was displayed on the screen. And how can I output the additional field of this element that I created?  
> **username_1**: Yes you can get entry by this $entry = Entry::findOne($id), and then if you want to get additional field just return $entry->additionalField  
> **username_0**: And I used this construction: $price = Entry::findOne(25)->GetFieldValue('priceProduct'). In principle, it is clear how to find out the values of the fields of a certain element. Please tell me, what kind of construction should I use to sort through all the elements of a certain section?  
> **username_1**: Oh If you want to find element on certain section you can do like this $entries = Entry::find()->section('sectionName')->all() you can put the orderBy if you want to do some sorting example like this Entry::find()->section('sectionName')->orderBy('title')all()  

---

