Answers.Body: <p>For a form that would do both RSVP and cancel RSVP, you could do the following:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set currentUserId = currentUser.id %}
  
  {% set currentAttendees = event.eventAttendees.ids() %}
  
  {# Is the current user attending? #}
  {% set attending = currentUserId in currentAttendees %}
  
  {# If the current user is attending, we remove its ID from the attendees (cancel action) #}
  {# If the current user is not attending, we add its ID to the attendees (rsvp action) #}
  {% set attendees = attending ? currentAttendees | filter(v =&gt; v != currentUserId) : currentAttendees | merge([currentUserId]) %}
  
  &lt;form method="post" accept-charset="UTF-8"&gt;
      {{ csrfInput() }}
      {{ actionInput('entries/save-entry') }}
      {{ hiddenInput('entryId', event.id) }}
      &lt;input type="hidden" name="fields[eventAttendees][]"&gt;
      {% for attendee in attendees %}
          &lt;input type="hidden" name="fields[eventAttendees][]" value="{{ attendee }}"&gt;
      {% endfor %}
      &lt;button type="submit" class="btn btn-primary"&gt;{{ attending ? 'Cancel RSVP' : 'RSVP' }}&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-28T21:27:12.757Z
Answers.Text: Wow, amazing. If you have some time to help more, I have two issues. First is if the user is logged out, I get `Impossible to access an attribute ("id") on a null variable.`
  
  Second, unfortunately the 'Cancel RSVP' doesn't actually remove the user from the entry.
  
  Very much appreciate any further help you can give.
Answers.UserDisplayName: revlis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-29T08:13:17.827Z
Answers.Text: @tommysvr This would only work for actual users and would require them to be registered and logged in otherwise Craft/you wouldn't know who is trying to RSVP for an event. In regard to cancelation, I just tried and (with a registerer, logged in user) it seems to work fine. Are you having issues with a logged in user?
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-29T12:26:40.513Z
Answers.Text: Ah. Of course - I will add a check for logged in.
  
  Yeah, logged in I am able to RSVP ok but when that changes to Cancel, nothing happens after clicking the button.
  
  This my full event query: https://gist.github.com/tommysvr/1d512eaf8528daec9c097a7622dfd8ac
Answers.UserDisplayName: revlis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-29T13:00:19.543Z
Answers.Text: @tommysvr Ooops... Just spotted the error and I have edited the code above (new line after `hiddenInput`). It should work fine now.
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-29T13:10:23.713Z
Answers.Text: Amazing. Thanks so much - seriously appreciate it!
Answers.UserDisplayName: revlis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-28T13:34:10.127Z
Answers.LastActivityDate: 2020-01-29T12:59:31.593Z
Answers.LastEditDate: 2020-01-29T12:59:31.593Z
Answers.UserDisplayName: Oli
Answers.Body: <p>You could do this:</p>
  
  <pre class="lang-twig prettyprint-override"><code>&lt;form method="post" accept-charset="UTF-8"&gt;
      {{ csrfInput() }}
      {{ actionInput('entries/save-entry') }}
      {{ hiddenInput('entryId', event.id) }}
      {% for user in event.eventAttendees %}
          &lt;input type="hidden" name="fields[eventAttendees][]" value="{{ user.id }}"&gt;
      {% endfor %}
      &lt;input type="hidden" name="fields[eventAttendees][]" value="{{ currentUser.id }}"&gt;
      &lt;button type="submit" class="btn btn-primary"&gt;RSVP&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The <code>for</code> loop in there is to ensure that you do not remove any value (users) that have already RSVP. This is similar to <a href="https://docs.craftcms.com/v3/assets-fields.html#uploading-files-from-front-end-entry-forms" rel="nofollow noreferrer">Uploading Files from Front-End Entry Forms</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-28T11:46:31.880Z
Answers.Text: Perfect! Thanks @Oli, that did the job!
Answers.UserDisplayName: revlis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-28T11:51:06.573Z
Answers.Text: I wonder if it would be too cheeky to now get a cancel RSVP version :D
  
  I suppose that would have to check if the user.id exists in the field and then show a different form to do the opposite...
Answers.UserDisplayName: revlis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-28T10:53:48.483Z
Answers.LastActivityDate: 2020-01-28T10:53:48.483Z
Answers.UserDisplayName: Oli
Body: <p>I'm trying to add an RSVP button for users to register their attendance at my events. Once they click the button, I would like them to be added to a field called <code>eventAttendees</code> on the event entry.</p>
  
  <p>My guess is this could be done as a form submission, and this is as far as I've gotten before getting a bit lost. I'm not sure how to pass the user ID to the entry.</p>
  
  <pre><code>&lt;form method="post" accept-charset="UTF-8"&gt;
    {{ csrfInput() }}
    {{ hiddenInput('entryId', event.id) }}
    {{ hiddenInput('userId', currentUser.id) }}
    &lt;input type="hidden" name="fields[eventAttendees][]"&gt;
    &lt;button type="submit" class="btn btn-primary"&gt;RSVP&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Would appreciate any help!</p>
  
Tags: <entries><users><field>
Title: Add user to entry user field [RSVP to Event]
ThreadText: # Add user to entry user field [RSVP to Event]
  
  (Asked by: username_0 on 2020-01-28)
  
  I'm trying to add an RSVP button for users to register their attendance at my events. Once they click the button, I would like them to be added to a field called `eventAttendees` on the event entry.
  
  My guess is this could be done as a form submission, and this is as far as I've gotten before getting a bit lost. I'm not sure how to pass the user ID to the entry.
  
  ```
  <form method="post" accept-charset="UTF-8">
    {{ csrfInput() }}
    {{ hiddenInput('entryId', event.id) }}
    {{ hiddenInput('userId', currentUser.id) }}
    <input type="hidden" name="fields[eventAttendees][]">
    <button type="submit" class="btn btn-primary">RSVP</button>
  </form>
  
  ```
  
  Would appreciate any help!
  
  **Tags:** entries, users, field
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-01-28. Score: 3 | Accepted answer
  
  For a form that would do both RSVP and cancel RSVP, you could do the following:
  
  ```twig
  {% set currentUserId = currentUser.id %}
  
  {% set currentAttendees = event.eventAttendees.ids() %}
  
  {# Is the current user attending? #}
  {% set attending = currentUserId in currentAttendees %}
  
  {# If the current user is attending, we remove its ID from the attendees (cancel action) #}
  {# If the current user is not attending, we add its ID to the attendees (rsvp action) #}
  {% set attendees = attending ? currentAttendees | filter(v => v != currentUserId) : currentAttendees | merge([currentUserId]) %}
  
  <form method="post" accept-charset="UTF-8">
      {{ csrfInput() }}
      {{ actionInput('entries/save-entry') }}
      {{ hiddenInput('entryId', event.id) }}
      <input type="hidden" name="fields[eventAttendees][]">
      {% for attendee in attendees %}
          <input type="hidden" name="fields[eventAttendees][]" value="{{ attendee }}">
      {% endfor %}
      <button type="submit" class="btn btn-primary">{{ attending ? 'Cancel RSVP' : 'RSVP' }}</button>
  </form>
  
  ```
  
  > **Comments:**
  > **username_0**: Wow, amazing. If you have some time to help more, I have two issues. First is if the user is logged out, I get `Impossible to access an attribute ("id") on a null variable.`  Second, unfortunately the 'Cancel RSVP' doesn't actually remove the user from the entry.  Very much appreciate any further help you can give.  
  > **username_1**: @tommysvr This would only work for actual users and would require them to be registered and logged in otherwise Craft/you wouldn't know who is trying to RSVP for an event. In regard to cancelation, I just tried and (with a registerer, logged in user) it seems to work fine. Are you having issues with a logged in user?  
  > **username_0**: Ah. Of course - I will add a check for logged in.  Yeah, logged in I am able to RSVP ok but when that changes to Cancel, nothing happens after clicking the button.  This my full event query: https://gist.github.com/tommysvr/1d512eaf8528daec9c097a7622dfd8ac  
  > **username_1**: @tommysvr Ooops... Just spotted the error and I have edited the code above (new line after `hiddenInput`). It should work fine now.  
  > **username_0**: Amazing. Thanks so much - seriously appreciate it!  
  
  ---
  
  ### Answer by username_1 on 2020-01-28. Score: 2
  
  You could do this:
  
  ```twig
  <form method="post" accept-charset="UTF-8">
      {{ csrfInput() }}
      {{ actionInput('entries/save-entry') }}
      {{ hiddenInput('entryId', event.id) }}
      {% for user in event.eventAttendees %}
          <input type="hidden" name="fields[eventAttendees][]" value="{{ user.id }}">
      {% endfor %}
      <input type="hidden" name="fields[eventAttendees][]" value="{{ currentUser.id }}">
      <button type="submit" class="btn btn-primary">RSVP</button>
  </form>
  
  ```
  
  The `for` loop in there is to ensure that you do not remove any value (users) that have already RSVP. This is similar to [Uploading Files from Front-End Entry Forms](https://docs.craftcms.com/v3/assets-fields.html#uploading-files-from-front-end-entry-forms)
  
  > **Comments:**
  > **username_0**: Perfect! Thanks @username_1, that did the job!  
  > **username_0**: I wonder if it would be too cheeky to now get a cancel RSVP version :D  I suppose that would have to check if the user.id exists in the field and then show a different form to do the opposite...  
  
  ---
  
  