<p>You would need to do this with a plugin that provides a custom <a href="https://craftcms.com/docs/plugins/controllers" rel="nofollow">controller</a>, which looks at the post data and modifies the entries’ Users fields accordingly.</p>

<p>The controller action would look something like this:</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php

$entryIds = craft()-&gt;request-&gt;getPost('entries');

if ($entryIds &amp;&amp; is_array($entryIds))
{
    // Get the current user's ID
    $currentUserId = craft()-&gt;userSession-&gt;getUser()-&gt;id;

    // Fetch the entries
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
    $criteria-&gt;id = $entryIds;
    $entries = $criteria-&gt;find();

    foreach ($entries as $entry)
    {
        // Update the Users field value with the current user's ID
        $userIds = $entry-&gt;myUsersField-&gt;ids();
        $userIds[] = $currentUserId;

        $entry-&gt;setContentFromPost(array(
            'myUsersField' =&gt; $userIds;
        ));

        craft()-&gt;entries-&gt;saveEntry($entry);
    }
}
</code></pre>

CC BY-SA 3.0
2016-04-04T15:09:06.530Z
Thanks for the reply, Brandon. What I meant was a "Users field type", not a field in a user's profile. What I am trying to do is update a Users field in a channel entry, with a front end form that would add a logged in user's ID to  a specific channel entry, via a Users field.
Roi Agneta
CC BY-SA 3.0
2016-04-04T23:00:41.700Z
@RoiAgneta Ah sorry, misunderstood. I just updated my answer :)
Brandon Kelly
CC BY-SA 3.0
2016-04-02T14:20:27.747Z
2016-04-04T22:59:35.587Z
2016-04-04T22:59:35.587Z
Brandon Kelly
<p>Is it possible to update an entry’s Users field from the front-end? If so, how would you go about it?</p>

<p>The situation is this: training events are created and then posted for members to bid on for a limited number of spots. Logged in members review the available events and add their name (would use their ID) to each event of interest. Admins then manually chose a select few from all submissions.</p>

<p>I know I can do this by adding their IDs to a text field in the event section, and then using Split to create an array, but the Users field approach is much more elegant and offers more flexibility.</p>

<users><relations>
How to add a user to a Users field from front-end form?
# How to add a user to a Users field from front-end form?

(Asked by: username_0 on 2016-04-01)

Is it possible to update an entry’s Users field from the front-end? If so, how would you go about it?

The situation is this: training events are created and then posted for members to bid on for a limited number of spots. Logged in members review the available events and add their name (would use their ID) to each event of interest. Admins then manually chose a select few from all submissions.

I know I can do this by adding their IDs to a text field in the event section, and then using Split to create an array, but the Users field approach is much more elegant and offers more flexibility.

**Tags:** users, relations


---

## 1 answers

### Answer by username_1 on 2016-04-02. Score: 2

You would need to do this with a plugin that provides a custom controller, which looks at the post data and modifies the entries’ Users fields accordingly.
The controller action would look something like this:
request->getPost('entries');

if ($entryIds && is_array($entryIds))
{
    // Get the current user's ID
    $currentUserId = craft()->userSession->getUser()->id;

    // Fetch the entries
    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->id = $entryIds;
    $entries = $criteria->find();

    foreach ($entries as $entry)
    {
        // Update the Users field value with the current user's ID
        $userIds = $entry->myUsersField->ids();
        $userIds[] = $currentUserId;

        $entry->setContentFromPost(array(
            'myUsersField' => $userIds;
        ));

        craft()->entries->saveEntry($entry);
    }
}

> **Comments:**
> **username_0**: Thanks for the reply, Brandon. What I meant was a "Users field type", not a field in a user's profile. What I am trying to do is update a Users field in a channel entry, with a front end form that would add a logged in user's ID to  a specific channel entry, via a Users field.  
> **username_1**: @RoiAgneta Ah sorry, misunderstood. I just updated my answer :)  

---

