Answers.Body: <p>Here is what I came up with that seems to be working well. Please let me know where there is room for improvement so others (including myself) can benefit!</p>
  
  <p><strong>JS</strong></p>
  
  <pre><code>var arr = [];
  var data = {};
  $('.remove').click(function (event) {
      var clicked = $(this).data('id');
  
      $('input[name="fields[myEntriesField][]"]').each(function (key, val) {
          var value = $(val).val();
          arr.push(value);
      });
  
      // Check if we are sending anything.
      if (arr.length == 0) {
          data.arr = null;
      } else {
          data.arr = arr;
      }
  
  onRemove(data, function (response) {
      // response
  });
  
  function onRemove(data, callback) {
      data[window.csrfTokenName] = window.csrfTokenValue;
      ...
      // ajax
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code> $userModel-&gt;getContent()-&gt;setAttributes([
       ...
       'myEntriesField' =&gt; craft()-&gt;request-&gt;getPost('fieldHandle'),
   ]);
  </code></pre>
  
  <p>Now if I click the "Delete" button, each entry is removed from the user's profile. If I delete the last entry, I pass <code>null</code> to the controller. This tells Craft that there are no entires to save in the entries field.</p>
  
  <p>Hope this helps someone else!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-20T19:03:57.957Z
Answers.LastActivityDate: 2017-05-20T19:03:57.957Z
Answers.UserDisplayName: Damon
Body: <p>I have an entries field that lives on a user's profile. I am using ajax to add entries to this field. </p>
  
  <p>Example: "Click to add to favorites". Everything works great.</p>
  
  <p>Now I am trying to delete an entry from the entires field, and I'm not quite sure how to pass the right data back to my controller.</p>
  
  <p>Here is what my js looks like:</p>
  
  <pre><code>var data = [];
  $('.remove').click(function (event) {
      var clicked = $(this).data('id');
  
      $('input[name="fields[myEntriesField][]"]').each(function (key, val) {
          var value = $(val).val();
  
      if (clicked != value) {
          data.push(value);
      }});
  });
  
      ...
  
  onRemove(data, function (response) {
      // response
  });
  
  function onRemove(data, callback) {
      data[window.csrfTokenName] = window.csrfTokenValue;
      ...
      // ajax
  }
  </code></pre>
  
  <p>What I am doing here is adding all of the entry id's to my <code>data</code> array <em>EXCEPT</em> the one that I clicked on. I was going to pass that array to my controller.</p>
  
  <p>Let's say I have 3 entries in my entries field. This is what is returned:</p>
  
  <pre><code>["1131", "1363"]
  </code></pre>
  
  <p>It's every entry <em>except</em> the one I clicked on.</p>
  
  <p>In my controller I was going to loop through the array (of entry ID's) and re-populate the entries field - save the user.</p>
  
  <p>I'm getting stuck trying to add the CSRF token - since I am passing in an array I guess I can't seem to append the CSRF token.</p>
  
  <p>The above is currently getting me this far:</p>
  
  <blockquote>
    <p>The CSRF token could not be verified</p>
  </blockquote>
  
  <p>If I log out all of the data just after I append the CSRF token, I get this:</p>
  
  <pre><code>["1131", "1363", _token: "TOKEN_NAME|TOKEN_VALUE"]
  </code></pre>
  
  <p>I've read through <a href="https://craftcms.stackexchange.com/questions/2013/how-do-you-delete-assets-from-a-frontend-form/2029#2029">this SO thread</a>. Which works - but I don't want to actually delete the entry - in essence I just need to re-populate the field.</p>
  
  <p>Thank you for any suggestions! </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've ben able to pass the value(s) to my controller, but now just struggling how to actually re-populate the entries field.</p>
  
  <p>This is what I'm passing into my controller:</p>
  
  <pre><code>data: Array(2)
      0:"1131"
      1:"1363"
  _token:"TOKEN_NAME|TOKEN_VALUE"
  </code></pre>
  
  <p>In my controller, I can access the values like this:</p>
  
  <pre><code>error_log(print_r(craft()-&gt;request-&gt;getPost('myField'), true));
  
  Array(
      [0] =&gt; 1131
      [1] =&gt; 1363
  )
  </code></pre>
  
  <p>Now just to repopulate the entries field?</p>
  
  <pre><code>// fields[myEntriesField] is an entries field type.
  $userModel-&gt;getContent()-&gt;setAttributes(array(
      'fields[myEntriesField]' =&gt; , ??? 
  ));
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-20T16:19:02.193Z
Comments.Text: Have you tried creating a separate root element (`var data = { data: [] }`), and then adding the IDs to `data.data`? You'll also need to modify your controller, of course.
Comments.UserDisplayName: Stephen Lewis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-20T16:32:43.880Z
Comments.Text: Yep. I'm successfully passing in the data now. Just stuck on re-populating the field. I've updated my question to show where I'm at.
Comments.UserDisplayName: Damon
Tags: <plugin-development>
Title: How can I delete entries from an entries field from a front-end from?
ThreadText: # How can I delete entries from an entries field from a front-end from?
  
  (Asked by: username_0 on 2017-05-19)
  
  I have an entries field that lives on a user's profile. I am using ajax to add entries to this field.
  
  Example: "Click to add to favorites". Everything works great.
  
  Now I am trying to delete an entry from the entires field, and I'm not quite sure how to pass the right data back to my controller.
  
  Here is what my js looks like:
  
  ```
  var data = [];
  $('.remove').click(function (event) {
      var clicked = $(this).data('id');
  
      $('input[name="fields[myEntriesField][]"]').each(function (key, val) {
          var value = $(val).val();
  
      if (clicked != value) {
          data.push(value);
      }});
  });
  
      ...
  
  onRemove(data, function (response) {
      // response
  });
  
  function onRemove(data, callback) {
      data[window.csrfTokenName] = window.csrfTokenValue;
      ...
      // ajax
  }
  
  ```
  
  What I am doing here is adding all of the entry id's to my `data` array *EXCEPT* the one that I clicked on. I was going to pass that array to my controller.
  
  Let's say I have 3 entries in my entries field. This is what is returned:
  
  ```
  ["1131", "1363"]
  
  ```
  
  It's every entry *except* the one I clicked on.
  
  In my controller I was going to loop through the array (of entry ID's) and re-populate the entries field - save the user.
  
  I'm getting stuck trying to add the CSRF token - since I am passing in an array I guess I can't seem to append the CSRF token.
  
  The above is currently getting me this far:
  
  > The CSRF token could not be verified
  
  If I log out all of the data just after I append the CSRF token, I get this:
  
  ```
  ["1131", "1363", _token: "TOKEN_NAME|TOKEN_VALUE"]
  
  ```
  
  I've read through [this SO thread](https://craftcms.stackexchange.com/questions/2013/how-do-you-delete-assets-from-a-frontend-form/2029#2029). Which works - but I don't want to actually delete the entry - in essence I just need to re-populate the field.
  
  Thank you for any suggestions!
  
  **EDIT**
  
  I've ben able to pass the value(s) to my controller, but now just struggling how to actually re-populate the entries field.
  
  This is what I'm passing into my controller:
  
  ```
  data: Array(2)
      0:"1131"
      1:"1363"
  _token:"TOKEN_NAME|TOKEN_VALUE"
  
  ```
  
  In my controller, I can access the values like this:
  
  ```
  error_log(print_r(craft()->request->getPost('myField'), true));
  
  Array(
      [0] => 1131
      [1] => 1363
  )
  
  ```
  
  Now just to repopulate the entries field?
  
  ```
  // fields[myEntriesField] is an entries field type.
  $userModel->getContent()->setAttributes(array(
      'fields[myEntriesField]' => , ??? 
  ));
  
  ```
  
  **Tags:** plugin-development
  
  > **Comments:**
  > **username_1**: Have you tried creating a separate root element (`var data = { data: [] }`), and then adding the IDs to `data.data`? You'll also need to modify your controller, of course.  
  > **username_0**: Yep. I'm successfully passing in the data now. Just stuck on re-populating the field. I've updated my question to show where I'm at.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-05-20. Score: 2
  
  Here is what I came up with that seems to be working well. Please let me know where there is room for improvement so others (including myself) can benefit!
  
  **JS**
  
  ```
  var arr = [];
  var data = {};
  $('.remove').click(function (event) {
      var clicked = $(this).data('id');
  
      $('input[name="fields[myEntriesField][]"]').each(function (key, val) {
          var value = $(val).val();
          arr.push(value);
      });
  
      // Check if we are sending anything.
      if (arr.length == 0) {
          data.arr = null;
      } else {
          data.arr = arr;
      }
  
  onRemove(data, function (response) {
      // response
  });
  
  function onRemove(data, callback) {
      data[window.csrfTokenName] = window.csrfTokenValue;
      ...
      // ajax
  }
  
  ```
  
  **Controller**
  
  ```
   $userModel->getContent()->setAttributes([
       ...
       'myEntriesField' => craft()->request->getPost('fieldHandle'),
   ]);
  
  ```
  
  Now if I click the "Delete" button, each entry is removed from the user's profile. If I delete the last entry, I pass `null` to the controller. This tells Craft that there are no entires to save in the entries field.
  
  Hope this helps someone else!
  
  
  ---
  
  