ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Safecracker - how to set multiple categories in a hidden field?

July 25, 2011 8:37pm

Subscribe [3]
  • #1 / Jul 25, 2011 8:37pm

    David Hyland

    210 posts

    Greetings

    In a Safecracker form I have a hidden field where I’m setting a Category ID via JS based on the user’s selections in the form. It all works fine when just one category ID is added to the value parameter but I can’t figure out how to get multiple categories assigned.

    Here’s my input field:

    <input type="hidden" name="category[]" id="categories" value="" />

    I’ve tried comma and pipe separated values but neither works. What is Safecracker looking for when it processes the form in order to get it to add the multiple values into the category_posts table?

    Or is the answer to have multiple hidden fields for each one?

  • #2 / Jul 26, 2011 12:44am

    Dave @ Exp:resso

    465 posts

    Well its an array, so yes it would mostly likely work if you just duplicated that hidden field with JS, with a different value each time

  • #3 / Jul 26, 2011 12:13pm

    David Hyland

    210 posts

    Yeah, that’s what I ended up doing 😊

  • #4 / Jul 26, 2011 1:33pm

    Kyle Cotter

    730 posts

    Thanks CrescendoNZ for the help here.

    Hey David,

    Duplicating the hidden field will work, but I’m just gonna throw this out to see if it applies to your situation.

    You can do something like

    {category_menu}
        <label for="categories">Categories</label>
        <select name="category[]" id="categories" size="4" multiple="multiple">
    
        {select_options}
        </select>
    {/category_menu}

    Where you have multiple=“multiple” set on the select field. Now, I know you are using hidden inputs, but just in case the ability to use a <select> was an option, I figured I’d throw it out. If not, then duplicating inputs will work.

    Here is some more information on categories from the docs.

    Hope this helps.

    Thanks!

  • #5 / Jul 26, 2011 1:38pm

    David Hyland

    210 posts

    Thanks for your thoughts Kyle but in this case the select is not an option as the categories are dynamically worked out based on other user inputs and selections in the form.

    Cheers 😊

  • #6 / Jul 26, 2011 1:40pm

    Kyle Cotter

    730 posts

    Yeah, I figured it was probably more complex than that. In that case, stick with the duplicating hidden fields then.

    Hope all goes well on the project!

    Feel free to post again as needed!

    Thanks.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases