<p>You can't solve that with one input alone but you can make use of hidden fields (like craft does)
Your solution would be 1 visible dummy checkbox and multiple hidden "real" inputs.. So your visible dummy checkbox triggers a javascript event, that sets the values of your hidden.
Here is the syntax</p>

<pre><code>&lt;input type="checkbox" id="user-type-select-{{ userType|kebab }}" /&gt;
</code></pre>

<p>You have to add a javascript/jquery onchange event that sets the values of your hidden fields</p>

<pre><code>&lt;input type="hidden" id="someId1" name="fields[hasUserTypes][]" value="someIntegerValue1" /&gt; 
&lt;input type="hidden" id="someId2" name="fields[hasUserTypes][]" value="someIntegerValue2" /&gt; 
&lt;input type="hidden" id="someId3" name="fields[hasUserTypes][]" value="someIntegerValue3" /&gt; 
</code></pre>

<p>in your php part your <code>$_POST</code> variable would be looking like</p>

<pre><code>array(
    ....
    'fields' =&gt; array(
        'hasUserTypes' =&gt; array(
            someIntegerValue1, someIntegerValue2, someIntegerValue3
        ),             
    ),
    ....
);
</code></pre>

<p>If you have (let us say) 3 visible checkboxes and each of it contains 2-3 relation presets you would like to store you have to change the values of your 2-3 hidden fields depending on what the user has selected. If you right click on craft cp pages and inspect the code you'll see craft does exactly the same, for every relation you select one <code>&lt;input type="hidden"&gt;</code> is created with the value of the id from the related entry</p>

CC BY-SA 3.0
2017-11-10T09:13:08.683Z
To bad this isn't as easy as I thought. But your solutions should work. I could even generate the hidden fields in JavaScript I suppose and add it to the HTML depending on the amount of values I'd like to save.
Tom De Smet
CC BY-SA 3.0
2017-11-10T09:21:15.103Z
well - actually it is easy. You only need a few lines and if you don`t refuse to try angular you can do it within 3-4 lines
Robin Schambach
CC BY-SA 3.0
2017-11-08T18:22:09.723Z
2017-11-08T18:22:09.723Z
Robin Schambach
<p>I have a frontend form and would like to save multiple related entries by checking 1 checkbox.
I have this working to save 1 related entry:</p>

<pre><code>&lt;input type="checkbox" id="user-type-select-{{ userType|kebab }}" name="fields[hasUserTypes][]" value="{{ userType.id }}" /&gt;
</code></pre>

<p>But how do I pass multiple <code>userType.id</code> values in there? I tried various syntaxes such as comma seperated, enclosed in brackets like an array or a comma seperated string with the <code>|hash</code> filter as seen <a href="https://github.com/craftcms/contact-form" rel="nofollow noreferrer">here</a> but nothing worked.</p>

<p>What is the correct sytax?</p>

<relatedto><saveentry><frontend-form>
How to save multiple related entries on the frontend with 1 checkbox?
# How to save multiple related entries on the frontend with 1 checkbox?

(Asked by: username_0 on 2017-11-08)

I have a frontend form and would like to save multiple related entries by checking 1 checkbox.
I have this working to save 1 related entry:


But how do I pass multiple userType.id values in there? I tried various syntaxes such as comma seperated, enclosed in brackets like an array or a comma seperated string with the |hash filter as seen here but nothing worked.
What is the correct sytax?

**Tags:** relatedto, saveentry, frontend-form


---

## 1 answers

### Answer by username_1 on 2017-11-08. Score: 2 | Accepted answer

You can't solve that with one input alone but you can make use of hidden fields (like craft does)
Your solution would be 1 visible dummy checkbox and multiple hidden "real" inputs.. So your visible dummy checkbox triggers a javascript event, that sets the values of your hidden.
Here is the syntax


You have to add a javascript/jquery onchange event that sets the values of your hidden fields
 
 
 

in your php part your $_POST variable would be looking like
array(
    ....
    'fields' => array(
        'hasUserTypes' => array(
            someIntegerValue1, someIntegerValue2, someIntegerValue3
        ),             
    ),
    ....
);

If you have (let us say) 3 visible checkboxes and each of it contains 2-3 relation presets you would like to store you have to change the values of your 2-3 hidden fields depending on what the user has selected. If you right click on craft cp pages and inspect the code you'll see craft does exactly the same, for every relation you select one  is created with the value of the id from the related entry

> **Comments:**
> **username_0**: To bad this isn't as easy as I thought. But your solutions should work. I could even generate the hidden fields in JavaScript I suppose and add it to the HTML depending on the amount of values I'd like to save.  
> **username_1**: well - actually it is easy. You only need a few lines and if you don`t refuse to try angular you can do it within 3-4 lines  

---

