<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>

CC BY-SA 4.0
2020-01-28T21:27:12.757Z
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.
revlis
CC BY-SA 4.0
2020-01-29T08:13:17.827Z
@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?
Oli
CC BY-SA 4.0
2020-01-29T12:26:40.513Z
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
revlis
CC BY-SA 4.0
2020-01-29T13:00:19.543Z
@tommysvr Ooops... Just spotted the error and I have edited the code above (new line after `hiddenInput`). It should work fine now.
Oli
CC BY-SA 4.0
2020-01-29T13:10:23.713Z
Amazing. Thanks so much - seriously appreciate it!
revlis
CC BY-SA 4.0
2020-01-28T13:34:10.127Z
2020-01-29T12:59:31.593Z
2020-01-29T12:59:31.593Z
Oli
<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>

CC BY-SA 4.0
2020-01-28T11:46:31.880Z
Perfect! Thanks @Oli, that did the job!
revlis
CC BY-SA 4.0
2020-01-28T11:51:06.573Z
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...
revlis
CC BY-SA 4.0
2020-01-28T10:53:48.483Z
2020-01-28T10:53:48.483Z
Oli
<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>

<entries><users><field>
Add user to entry user field [RSVP to Event]
# 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.

  {{ csrfInput() }}
  {{ hiddenInput('entryId', event.id) }}
  {{ hiddenInput('userId', currentUser.id) }}
  
  RSVP


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:
{% 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]) %}


    {{ csrfInput() }}
    {{ actionInput('entries/save-entry') }}
    {{ hiddenInput('entryId', event.id) }}
    
    {% for attendee in attendees %}
        
    {% endfor %}
    {{ attending ? 'Cancel RSVP' : 'RSVP' }}

> **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:

    {{ csrfInput() }}
    {{ actionInput('entries/save-entry') }}
    {{ hiddenInput('entryId', event.id) }}
    {% for user in event.eventAttendees %}
        
    {% endfor %}
    
    RSVP


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

> **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...  

---

