Getting values from checkboxes

abeam's Avatar

abeam

16 Apr, 2015 08:53 PM

I'm trying to capture checkbox values and output in a comma separated list onto the page for each grouping. Basically, I'm making a program listing with courses and the semester offered (fall, spring, summer), but my output is showing as "fallspringsummer, summer" in some cases.

For some reason looping through multiple values has thrown me off programatically. Can you help point out my problem?

  1. 1 Posted by Ryan Griffith on 17 Apr, 2015 02:49 PM

    Ryan Griffith's Avatar

    Hi,

    It looks like you are definitely on the right track; however, I wanted to point out a couple of things:

    • Each checkbox is a separate <value> element within <offered>, so you would want to adjust the way you are obtaining the values for $semestersoffered
    • You using $courselistings instead of $courselisting when obtaining the offerings, so you will end up getting mixed results from other courses
    • There is a special property for loops, $foreach.hasNext that you can use to determine when to add a comma, thus simplifying your loop quite a bit
    • If you are running Cascade 6.10+, the Escape Tool can be used instead of the Serializer Tool to escape content

    That being said, give the following a try and let me know how it works out:

    ## if a semester is selected in the checkboxes, output the values
    #set ($semestersoffered = $_XPathTool.selectNodes($courselisting, "offered/value")) 
    #if ($semestersoffered.size() > 0)
        #foreach ($semester in $semestersoffered) 
            $_EscapeTool.xml($semester.value)#if ($foreach.hasNext ), #end
        #end
    #end
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by abeam on 17 Apr, 2015 07:07 PM

    abeam's Avatar

    Thanks! That's helpful.

    As a follow-up question. . . what if I wanted to set the results from that loop as a variable. In another section I'm wanting to take the values from pathways and use it as class names for the div wrapping the section. Tried adapting the code you provided for this, but I'm just getting the last value.

    #set ($pathtypes = $_XPathTool.selectNodes($courselisting, "pathway/value")) 
    #if ($pathtypes.size() > 0)
       #foreach ($paths in $pathtypes) 
            #set ($p = $_EscapeTool.xml($paths.value))
            #if ($foreach.hasNext ) #end   
        #end
    #end
    
    ## output pathways as .classes (e.g. "class="degree diploma certificate")
    ## will use script to show/hide based on end user input.
    <div class="${p}">
    
  3. 3 Posted by Ryan Griffith on 17 Apr, 2015 08:17 PM

    Ryan Griffith's Avatar

    Hi,

    To confirm, you would like to simply save the chosen values and output them within an element's class attribute? If so, you would want to use string concatenation.

    When you have a moment, give the following a try and let me know how it works out:

    #set ($pathwaysClass = "")
    #set ($pathtypes = $_XPathTool.selectNodes($courselisting, "pathway/value")) 
    #if ($pathtypes.size() > 0)
       #foreach ($paths in $pathtypes) 
            #set ($pathwaysClass = "${pathwaysClass} ${_EscapeTool.xml($paths.value)}")
        #end
    #end
    
    ## output pathways as .classes (e.g. "class="degree diploma certificate")
    ## will use script to show/hide based on end user input.
    <div class="${pathwaysClass}">
    

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by abeam on 20 Apr, 2015 03:49 PM

    abeam's Avatar

    This is perfect. Concatenating each class makes sense. Thanks for the help!

  5. abeam closed this discussion on 20 Apr, 2015 03:49 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM