<p>There are a few problems both with your form and custom controller action.</p>
<p>Your form is posting to the URL <code>/add/contact</code> via the <code>action</code> attribute, which – unless you've set up a custom route – is not going to be the actual URL for your controller action. I suggest reading up on <a href="https://craftcms.com/docs/4.x/extend/controllers.html#requesting-your-controller-action" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/extend/controllers.html#requesting-your-controller-action</a>, but in most cases you'll want to add an <em>action input</em> to your form via the <a href="https://craftcms.com/docs/4.x/dev/functions.html#actioninput" rel="nofollow noreferrer"><code>actionInput()</code></a> Twig function, like this:</p>
<pre><code>&lt;form method=&quot;post&quot;&gt;
    {{ actionInput('path/to/action') }}
&lt;/form&gt;
</code></pre>
<p>Note that there should not be an <code>action</code> attribute on the <code>&lt;form&gt;</code> when using an action input.</p>
<p>The path to your specific action depends on what the module's ID in your <code>config/app.php</code> is, but it's likely going to be something like <code>addmodule/add/start</code> (i.e. where <code>addmodule</code> is the module ID, <code>add</code> represents the <code>AddController</code> class and <code>contact</code> represents the <code>actionStart</code> method).</p>
<p>Since this is a POST request, chances are you'll also going to need to include a CSRF token with your request. The easiest way to do that is to use the <code>csrfInput()</code> Twig function, i.e.</p>
<pre><code>&lt;form method=&quot;post&quot;&gt;
    {{ actionInput('addmodule/add/start') }}
    {{ csrfInput() }}
&lt;/form&gt;
</code></pre>
<p>As for the custom controller's <code>actionStart()</code> method – it's currently not doing anything, just returning the <code>username</code> POST param back to the client.</p>
<p>To actually create an entry and save it, you'll need to create an Entry model, set the appropriate section ID and the <code>username</code> field on it, and save it via the <code>Craft::$app-&gt;getElements()</code> service.</p>
<p>Also note you seem to be using Craft 2 syntax for getting the <code>username</code> POST param, i.e. instead of <code>craft()-&gt;request-&gt;getPost('username')</code> it should look like this:</p>
<pre class="lang-php prettyprint-override"><code>$formContent = \Craft::$app-&gt;getRequest()-&gt;getBodyParam('username');
</code></pre>
<p>...or simpler:</p>
<pre class="lang-php prettyprint-override"><code>$formContent = $this-&gt;request-&gt;getBodyParam('username');
</code></pre>
<p>To actually create and save an entry, you'll need to do something like this:</p>
<pre class="lang-php prettyprint-override"><code>$entry = new Entry();
$entry-&gt;sectionId =&gt; 123;
$entry-&gt;setFieldValues([
    'username' =&gt; $username,
]);
$success = \Craft::$app-&gt;getElements()-&gt;saveElement($entry);
return $success;
</code></pre>
<p>Note that for adding a simple contact form to your site, there are some good alternatives to going all in with writing a custom module from scratch. For starters, you might want to take a look at the <a href="https://craftcms.com/knowledge-base/entry-form" rel="nofollow noreferrer">example front-end entry form</a> in the official docs – this could likely be adapted to fit your needs, and would completely negate the need to write a custom module in the first place.</p>
<p><strong>If you need the standard, front-end entry form to redirect to a particular URL after successfully submitting</strong>, you can use the <code>redirectInput()</code> Twig method to make the form redirect to any URL you like (the example form uses <code>redirectInput('viewentry/{slug}')</code> as an example).</p>
<p>The URL passed to <code>redirectInput</code> can even include dynamic values – such as <code>{slug}</code> in the example form, which would refer to the saved entry's slug. In other words, if you need to redirect the user to a specific URL that includes, for example, the value for a custom field on the entry they just created, you could do something like</p>
<pre><code>{{ redirectInput('some-url/{paymentMethod}) }}
</code></pre>
<p>...and the user would be redirected to a URL containing the value for a custom field <code>paymentMethod</code> on the entry.</p>
<p>You can even use Twig filters on the dynamic value, for example making sure it's in lowercase:</p>
<pre><code>{{ redirectInput('some-url/{paymentMethod|lower}) }}
</code></pre>
<p>Note that if you decide to use a standard front-end entry form and need to accept <em>anonymous</em> submissions (i.e. from users not logged in to Craft), you should probably install the first-party <a href="https://plugins.craftcms.com/guest-entries?craft4" rel="nofollow noreferrer">Guest Entries plugin</a>.</p>

CC BY-SA 4.0
2022-10-31T19:40:18.890Z
I'm afraid that the standard solution won't work for me. The fact is that after saving the record in the database, in my case, redirection to a specific URL should be performed, which will be formed based on what the user chooses. For example, if he chooses the WEBMONEY payment method, he will be redirected to the URL dedicated to the webmoney payment method. That's why I thought it should work as a module.
Dimi
CC BY-SA 4.0
2022-10-31T20:30:54.890Z
my task is this: A person came in, entered the data into the form, clicked submit. After that, you need to save this data and, based on what he typed in, construct a url to which he then redirects the user. That is why I decided that it is best to use the module, and in its controller to do all the necessary actions to process the entered data
Dimi
CC BY-SA 4.0
2022-11-01T00:13:55.497Z
@Dimi You can actually do that without a module or plugin, by adding a redirect input (using the `redirectInput()` Twig function) to the form containing a dynamic value – for example, the field handle for a custom field `paymentMethod` on the entry, that will be rendered in the URL that the user is redirected to after successfully submitting the form. I've added some notes on that to my answer.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-11-01T00:14:58.463Z
Also added a note about anonymous submissions using a standard front-end form, which does require a plugin (but [there's a first-party one](https://plugins.craftcms.com/guest-entries?craft4)).
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-11-02T21:05:56.997Z
And yet I did it through the module. Your post helped a lot. Did so: 1. I brought out the form in the twig template as you advised. I put all the necessary fields in the form. The action of the form pointed to nameidmodule/controller/action. 2. In the module controller, I did all the necessary actions with the received data. 3. In the module controller using Craft::$app->GetResponse()->redirect($myUrl) organized the redirection.
Dimi
CC BY-SA 4.0
2022-10-30T22:13:42.707Z
2022-11-01T00:17:39.663Z
2022-11-01T00:17:39.663Z
Mats Mikkel Rummelhoff
<p>please help me write a simple form for adding entries to the section.
I have a twig template in which there is a simple form:</p>
<pre><code>&lt;div&gt;
    &lt;form action=&quot;/add/contact/&quot; method=&quot;POST&quot;&gt;
        Enter you name
        &lt;input type=&quot;text&quot; name=&quot;username&quot; value=&quot;&quot;&gt;
        &lt;input type=&quot;submit&quot; value=&quot;submit&quot;&gt;
    &lt;/form&gt;
&lt;/div&gt;
</code></pre>
<p>I created a module that works at /add/contact/. Here is the code of this module:</p>
<pre><code>&lt;?php
namespace modules\add\controllers;

use Craft;
use craft\web\Controller;
use craft\elements\Entry;

class AddController extends Controller
{

    protected array|bool|int $allowAnonymous = true;

    public function actionStart() {

        $formContent = craft()-&gt;request-&gt;getPost('username');
        return $formContent;
        
    }
}
</code></pre>
<p>Unfortunately, but my code doesn't work. The value entered in the input is not displayed after the submit.
Why? Help me figure it out, please.</p>
<p>I also need help in redirecting after receiving and saving data. What is the best way to implement redirection?</p>

<craft3>
How to create a simple contact form?
# How to create a simple contact form?

(Asked by: username_0 on 2022-10-29)

please help me write a simple form for adding entries to the section.
I have a twig template in which there is a simple form:

    
        Enter you name
        
        
    


I created a module that works at /add/contact/. Here is the code of this module:
request->getPost('username');
        return $formContent;
        
    }
}

Unfortunately, but my code doesn't work. The value entered in the input is not displayed after the submit.
Why? Help me figure it out, please.
I also need help in redirecting after receiving and saving data. What is the best way to implement redirection?

**Tags:** craft3

---

## 1 answers

### Answer by username_1 on 2022-10-30. Score: 3 | Accepted answer

There are a few problems both with your form and custom controller action.
Your form is posting to the URL /add/contact via the action attribute, which – unless you've set up a custom route – is not going to be the actual URL for your controller action. I suggest reading up on https://craftcms.com/docs/4.x/extend/controllers.html#requesting-your-controller-action, but in most cases you'll want to add an action input to your form via the actionInput() Twig function, like this:

    {{ actionInput('path/to/action') }}


Note that there should not be an action attribute on the  when using an action input.
The path to your specific action depends on what the module's ID in your config/app.php is, but it's likely going to be something like addmodule/add/start (i.e. where addmodule is the module ID, add represents the AddController class and contact represents the actionStart method).
Since this is a POST request, chances are you'll also going to need to include a CSRF token with your request. The easiest way to do that is to use the csrfInput() Twig function, i.e.

    {{ actionInput('addmodule/add/start') }}
    {{ csrfInput() }}


As for the custom controller's actionStart() method – it's currently not doing anything, just returning the username POST param back to the client.
To actually create an entry and save it, you'll need to create an Entry model, set the appropriate section ID and the username field on it, and save it via the Craft::$app->getElements() service.
Also note you seem to be using Craft 2 syntax for getting the username POST param, i.e. instead of craft()->request->getPost('username') it should look like this:
$formContent = \Craft::$app->getRequest()->getBodyParam('username');

...or simpler:
$formContent = $this->request->getBodyParam('username');

To actually create and save an entry, you'll need to do something like this:
$entry = new Entry();
$entry->sectionId => 123;
$entry->setFieldValues([
    'username' => $username,
]);
$success = \Craft::$app->getElements()->saveElement($entry);
return $success;

Note that for adding a simple contact form to your site, there are some good alternatives to going all in with writing a custom module from scratch. For starters, you might want to take a look at the example front-end entry form in the official docs – this could likely be adapted to fit your needs, and would completely negate the need to write a custom module in the first place.
If you need the standard, front-end entry form to redirect to a particular URL after successfully submitting, you can use the redirectInput() Twig method to make the form redirect to any URL you like (the example form uses redirectInput('viewentry/{slug}') as an example).
The URL passed to redirectInput can even include dynamic values – such as {slug} in the example form, which would refer to the saved entry's slug. In other words, if you need to redirect the user to a specific URL that includes, for example, the value for a custom field on the entry they just created, you could do something like
{{ redirectInput('some-url/{paymentMethod}) }}

...and the user would be redirected to a URL containing the value for a custom field paymentMethod on the entry.
You can even use Twig filters on the dynamic value, for example making sure it's in lowercase:
{{ redirectInput('some-url/{paymentMethod|lower}) }}

Note that if you decide to use a standard front-end entry form and need to accept anonymous submissions (i.e. from users not logged in to Craft), you should probably install the first-party Guest Entries plugin.

> **Comments:**
> **username_0**: I'm afraid that the standard solution won't work for me. The fact is that after saving the record in the database, in my case, redirection to a specific URL should be performed, which will be formed based on what the user chooses. For example, if he chooses the WEBMONEY payment method, he will be redirected to the URL dedicated to the webmoney payment method. That's why I thought it should work as a module.  
> **username_0**: my task is this: A person came in, entered the data into the form, clicked submit. After that, you need to save this data and, based on what he typed in, construct a url to which he then redirects the user. That is why I decided that it is best to use the module, and in its controller to do all the necessary actions to process the entered data  
> **username_1**: @Dimi You can actually do that without a module or plugin, by adding a redirect input (using the `redirectInput()` Twig function) to the form containing a dynamic value – for example, the field handle for a custom field `paymentMethod` on the entry, that will be rendered in the URL that the user is redirected to after successfully submitting the form. I've added some notes on that to my answer.  
> **username_1**: Also added a note about anonymous submissions using a standard front-end form, which does require a plugin (but [there's a first-party one](https://plugins.craftcms.com/guest-entries?craft4)).  
> **username_0**: And yet I did it through the module. Your post helped a lot. Did so: 1. I brought out the form in the twig template as you advised. I put all the necessary fields in the form. The action of the form pointed to nameidmodule/controller/action. 2. In the module controller, I did all the necessary actions with the received data. 3. In the module controller using Craft::$app->GetResponse()->redirect($myUrl) organized the redirection.  

---

