Answers.Body: <p>You will need a custom plugin to do this.</p>
  
  <p>I'll assume that you're familiar with that process. </p>
  
  <p>You can build a form in your template like this, passing in the entry ID you want to add the user to and the user ID you want to add.</p>
  
  <pre><code>&lt;form method="post" accept-charset="UTF-8"&gt; 
      {{ getCsrfInput() }}
      &lt;input type="hidden" name="action" value="myPlugin/save"&gt;
      &lt;input type="hidden" name="redirect" value="/success"&gt;
  
      &lt;input type="hidden" name="elementId" value="{{ entry.id }}"&gt;
  
      &lt;input type="hidden" name="userId" value="{{ currentUser.id }}"&gt;
  
      &lt;button type="submit" &gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Then, you'll need a controller action in your plugin to which you submit a POST request from a form.</p>
  
  <pre><code>public function actionSave()
  {
      $this-&gt;requirePostRequest();
  
      // get all the post variables
      $elementId = craft()-&gt;request-&gt;getPost('elementId');
      $userId = craft()-&gt;request-&gt;getPost('userId');
  
      // set the name of the field you want to update
      $fieldHandle = 'myUserField';
  
      // based on the element and field vars, get the models for each
      // using the elements and fields services
      $element = craft()-&gt;elements-&gt;getElementById($elementId);
      $field = craft()-&gt;fields-&gt;getFieldByHandle($fieldHandle);
  
      // store the current ids into a variable
      $currentIds = $element-&gt;{$field-&gt;handle}-&gt;ids();
  
      // merge the new id into the current ids
      $newIds = array_merge($currentIds, (array) $userId); 
  
      // save the new array of ids, using the relations service
      craft()-&gt;relations-&gt;saveRelations($field, $element, $newIds);
  
      // redirect to the redirect url
      $this-&gt;redirectToPostedUrl();
  
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>It's very important that you follow the naming conventions in the docs. </p>
  
  <p>You controller class should be named MyPluginController, which would then make the action in the form myPlugin/save (controller and action get removed from the exposed plugin and action names). </p>
  
  <p>Also the redirect should be a page on the side where you want the user to go after being added, not pointing to your plugin.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-07T11:15:32.733Z
Answers.LastActivityDate: 2017-04-12T12:52:32.967Z
Answers.LastEditDate: 2017-04-12T12:52:32.967Z
Answers.UserDisplayName: Jeremy Gimbel
Answers.Body: <p>Hi Jeremy thanks for the help it is getting me on the right track what you have given me thanks :), However I still haven't quite solved it, I found documentation on how to use custom controllers. This is where I am at now.</p>
  
  <p>I have created a controller under the controllers folder in the plugins directory with the file name of myPlugin_myController.php</p>
  
  <p>This is the following code inside it.</p>
  
  <pre><code>&lt;?php
  class myPlugin_myController extends BaseController
  {
      public function actionSave()
      {
  
          $this-&gt;requirePostRequest();
  
          // get all the post variables
          $elementId = craft()-&gt;request-&gt;getPost('elementId');
          $userId = craft()-&gt;request-&gt;getPost('userId');
  
          // set the name of the field you want to update
          $fieldHandle = 'friends';
  
         // based on the element and field vars, get the models for each
         // using the elements and fields services
         $element = craft()-&gt;elements-&gt;getElementById($elementId);
         $field = craft()-&gt;fields-&gt;getFieldByHandle($fieldHandle);
  
         // store the current ids into a variable
         $currentIds = $element-&gt;{$field-&gt;handle}-&gt;ids();
  
         // merge the new id into the current ids
         $newIds = array_merge($currentIds, (array) $userId); 
  
         // save the new array of ids, using the relations service
         craft()-&gt;relations-&gt;saveRelations($field, $element, $newIds);
  
         // redirect to the redirect url
         $this-&gt;redirectToPostedUrl();
  
      }
  }
  ?&gt;
  </code></pre>
  
  <p>This is my code in an html file, it is submitting some data however it returns a 404 error.</p>
  
  <pre><code> &lt;form method="post" accept-charset="UTF-8"&gt; 
                {{ getCsrfInput() }}
                &lt;input type="hidden" name="action" value="myPlugin/myController/actionSave"&gt;
                &lt;input type="hidden" name="redirect" value="myPlugin/myController"&gt;
  
                &lt;input type="hidden" name="elementId" value="{{ entry.id }}"&gt;
  
                &lt;input type="hidden" name="userId" value="{{ currentUser.id }}"&gt;
  
                &lt;button type="submit" &gt;Submit&lt;/button&gt;
            &lt;/form&gt;
  </code></pre>
  
  <p>Do you know where I am going wrong now any tips or help is all appreciated! :)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-10T11:07:23.650Z
Answers.Text: You controller should be named MyPluginController, which would then make the action in the form myPlugin/save (controller and action get removed from the exposed action). Also the redirect should be a page on the side where you want the user to go after being added, not pointing to your plugin.
Answers.UserDisplayName: Jeremy Gimbel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-10T11:08:39.023Z
Answers.Text: Also, it'd be best to update your question with this, so that we can better help you.
Answers.UserDisplayName: Jeremy Gimbel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-10T17:49:10.290Z
Answers.Text: @Alistair: This isn't an answer, it's an extension of your original question. Please edit this information into the original question... we'll be deleting this non-answer.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-12T09:17:18.513Z
Answers.Text: @JeremyGimbel I have updated the code up the top, still no luck :/ but I think we are getting closer.
Answers.UserDisplayName: Alistair
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-12T12:53:00.807Z
Answers.Text: I updated my answer. Your controller class is still named incorrectly.
Answers.UserDisplayName: Jeremy Gimbel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-13T23:28:35.887Z
Answers.Text: @JeremyGimbel I'm not sure why its not working but it is still not working is there anything you can still see?
Answers.UserDisplayName: Alistair
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-13T23:48:19.810Z
Answers.Text: @JeremyGimbel I'm a bit suspicious of my controller not even being triggered at all
Answers.UserDisplayName: Alistair
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-14T03:32:37.017Z
Answers.Text: Based on the code in your question, you still haven't followed the correct naming conventions. Capitalization matters. This would very much prevent the controller from executing. I would suggest reviewing the docs (https://craftcms.com/docs/plugins/controllers) or perhaps examining some of the other available plugins (perhaps P&T's own contact form plugin) to compare your naming and structure.
Answers.UserDisplayName: Jeremy Gimbel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-08T08:15:09.580Z
Answers.LastActivityDate: 2017-04-08T08:15:09.580Z
Answers.UserDisplayName: Alistair
Body: <p>I have created a controller called myPlugin_Controller.php</p>
  
  <p>This is the following code inside it.</p>
  
  <pre><code> &lt;?php
  class myPluginController extends BaseController
  {
      public function actionSave()
      {
  
          $this-&gt;requirePostRequest();
  
          // get all the post variables
          $elementId = craft()-&gt;request-&gt;getPost('elementId');
          $userId = craft()-&gt;request-&gt;getPost('userId');
  
          // set the name of the field you want to update
          $fieldHandle = 'friends';
  
          // based on the element and field vars, get the models for each
          // using the elements and fields services
          $element = craft()-&gt;elements-&gt;getElementById($elementId);
          $field = craft()-&gt;fields-&gt;getFieldByHandle($fieldHandle);
  
          // store the current ids into a variable
          $currentIds = $element-&gt;{$field-&gt;handle}-&gt;ids();
  
          // merge the new id into the current ids
          $newIds = array_merge($currentIds, (array) $userId); 
  
          // save the new array of ids, using the relations service
          craft()-&gt;relations-&gt;saveRelations($field, $element, $newIds);
  
          // redirect to the redirect url
          $this-&gt;redirectToPostedUrl();
  
      }
  }
  ?&gt;
  </code></pre>
  
  <p>This is my code in an html file, it is submitting some data however it returns a 404 error.</p>
  
  <pre><code>   &lt;form method="post" accept-charset="UTF-8"&gt; 
                    {{ getCsrfInput() }}
                &lt;input type="hidden" name="action" value="myPlugin/Save"&gt;
                &lt;input type="hidden" name="redirect" value="/success"&gt;
  
                &lt;input type="hidden" name="elementId" value="{{ user.id }}"&gt;
  
                &lt;input type="hidden" name="userId" value="{{ currentUser.id }}"&gt;
  
                &lt;button type="submit" &gt;Submit&lt;/button&gt;
            &lt;/form&gt;
  </code></pre>
  
  <p>i'm not too sure where its going wrong now.</p>
  
  <p>Also if your wondering why its called user this is why
          {% set user = craft.users.username( craft.request.segment(2) ).first() %}
  Do you know where I am going wrong now any tips or help is all appreciated! :)</p>
  
  <p>Still not working I have looked at craft cms documentation online i've followed your steps and there steps. </p>
  
  <p>Is there anything you can still see that is wrong?</p>
  
Tags: <plugin-development><users>
Title: How do you programatically add a user to a user entry field
ThreadText: # How do you programatically add a user to a user entry field
  
  (Asked by: username_0 on 2017-04-07)
  
  I have created a controller called myPlugin\_Controller.php
  
  This is the following code inside it.
  
  ```
   <?php
  class myPluginController extends BaseController
  {
      public function actionSave()
      {
  
          $this->requirePostRequest();
  
          // get all the post variables
          $elementId = craft()->request->getPost('elementId');
          $userId = craft()->request->getPost('userId');
  
          // set the name of the field you want to update
          $fieldHandle = 'friends';
  
          // based on the element and field vars, get the models for each
          // using the elements and fields services
          $element = craft()->elements->getElementById($elementId);
          $field = craft()->fields->getFieldByHandle($fieldHandle);
  
          // store the current ids into a variable
          $currentIds = $element->{$field->handle}->ids();
  
          // merge the new id into the current ids
          $newIds = array_merge($currentIds, (array) $userId); 
  
          // save the new array of ids, using the relations service
          craft()->relations->saveRelations($field, $element, $newIds);
  
          // redirect to the redirect url
          $this->redirectToPostedUrl();
  
      }
  }
  ?>
  
  ```
  
  This is my code in an html file, it is submitting some data however it returns a 404 error.
  
  ```
     <form method="post" accept-charset="UTF-8"> 
                    {{ getCsrfInput() }}
                <input type="hidden" name="action" value="myPlugin/Save">
                <input type="hidden" name="redirect" value="/success">
  
                <input type="hidden" name="elementId" value="{{ user.id }}">
  
                <input type="hidden" name="userId" value="{{ currentUser.id }}">
  
                <button type="submit" >Submit</button>
            </form>
  
  ```
  
  i'm not too sure where its going wrong now.
  
  Also if your wondering why its called user this is why
  {% set user = craft.users.username( craft.request.segment(2) ).first() %}
  Do you know where I am going wrong now any tips or help is all appreciated! :)
  
  Still not working I have looked at craft cms documentation online i've followed your steps and there steps.
  
  Is there anything you can still see that is wrong?
  
  **Tags:** plugin-development, users
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-04-07. Score: 3
  
  You will need a custom plugin to do this.
  
  I'll assume that you're familiar with that process.
  
  You can build a form in your template like this, passing in the entry ID you want to add the user to and the user ID you want to add.
  
  ```
  <form method="post" accept-charset="UTF-8"> 
      {{ getCsrfInput() }}
      <input type="hidden" name="action" value="myPlugin/save">
      <input type="hidden" name="redirect" value="/success">
  
      <input type="hidden" name="elementId" value="{{ entry.id }}">
  
      <input type="hidden" name="userId" value="{{ currentUser.id }}">
  
      <button type="submit" >Submit</button>
  </form>
  
  ```
  
  Then, you'll need a controller action in your plugin to which you submit a POST request from a form.
  
  ```
  public function actionSave()
  {
      $this->requirePostRequest();
  
      // get all the post variables
      $elementId = craft()->request->getPost('elementId');
      $userId = craft()->request->getPost('userId');
  
      // set the name of the field you want to update
      $fieldHandle = 'myUserField';
  
      // based on the element and field vars, get the models for each
      // using the elements and fields services
      $element = craft()->elements->getElementById($elementId);
      $field = craft()->fields->getFieldByHandle($fieldHandle);
  
      // store the current ids into a variable
      $currentIds = $element->{$field->handle}->ids();
  
      // merge the new id into the current ids
      $newIds = array_merge($currentIds, (array) $userId); 
  
      // save the new array of ids, using the relations service
      craft()->relations->saveRelations($field, $element, $newIds);
  
      // redirect to the redirect url
      $this->redirectToPostedUrl();
  
  }
  
  ```
  
  **EDIT:**
  
  It's very important that you follow the naming conventions in the docs.
  
  You controller class should be named MyPluginController, which would then make the action in the form myPlugin/save (controller and action get removed from the exposed plugin and action names).
  
  Also the redirect should be a page on the side where you want the user to go after being added, not pointing to your plugin.
  
  
  ---
  
  ### Answer by username_0 on 2017-04-08. Score: 0
  
  Hi username_1 thanks for the help it is getting me on the right track what you have given me thanks :), However I still haven't quite solved it, I found documentation on how to use custom controllers. This is where I am at now.
  
  I have created a controller under the controllers folder in the plugins directory with the file name of myPlugin\_myController.php
  
  This is the following code inside it.
  
  ```
  <?php
  class myPlugin_myController extends BaseController
  {
      public function actionSave()
      {
  
          $this->requirePostRequest();
  
          // get all the post variables
          $elementId = craft()->request->getPost('elementId');
          $userId = craft()->request->getPost('userId');
  
          // set the name of the field you want to update
          $fieldHandle = 'friends';
  
         // based on the element and field vars, get the models for each
         // using the elements and fields services
         $element = craft()->elements->getElementById($elementId);
         $field = craft()->fields->getFieldByHandle($fieldHandle);
  
         // store the current ids into a variable
         $currentIds = $element->{$field->handle}->ids();
  
         // merge the new id into the current ids
         $newIds = array_merge($currentIds, (array) $userId); 
  
         // save the new array of ids, using the relations service
         craft()->relations->saveRelations($field, $element, $newIds);
  
         // redirect to the redirect url
         $this->redirectToPostedUrl();
  
      }
  }
  ?>
  
  ```
  
  This is my code in an html file, it is submitting some data however it returns a 404 error.
  
  ```
   <form method="post" accept-charset="UTF-8"> 
                {{ getCsrfInput() }}
                <input type="hidden" name="action" value="myPlugin/myController/actionSave">
                <input type="hidden" name="redirect" value="myPlugin/myController">
  
                <input type="hidden" name="elementId" value="{{ entry.id }}">
  
                <input type="hidden" name="userId" value="{{ currentUser.id }}">
  
                <button type="submit" >Submit</button>
            </form>
  
  ```
  
  Do you know where I am going wrong now any tips or help is all appreciated! :)
  
  > **Comments:**
  > **username_1**: You controller should be named MyPluginController, which would then make the action in the form myPlugin/save (controller and action get removed from the exposed action). Also the redirect should be a page on the side where you want the user to go after being added, not pointing to your plugin.  
  > **username_1**: Also, it'd be best to update your question with this, so that we can better help you.  
  > **username_2**: @username_0: This isn't an answer, it's an extension of your original question. Please edit this information into the original question... we'll be deleting this non-answer.  
  > **username_0**: @username_1 I have updated the code up the top, still no luck :/ but I think we are getting closer.  
  > **username_1**: I updated my answer. Your controller class is still named incorrectly.  
  > **username_0**: @username_1 I'm not sure why its not working but it is still not working is there anything you can still see?  
  > **username_0**: @username_1 I'm a bit suspicious of my controller not even being triggered at all  
  > **username_1**: Based on the code in your question, you still haven't followed the correct naming conventions. Capitalization matters. This would very much prevent the controller from executing. I would suggest reviewing the docs (https://craftcms.com/docs/plugins/controllers) or perhaps examining some of the other available plugins (perhaps P&T's own contact form plugin) to compare your naming and structure.  
  
  ---
  
  