We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8168
    • 1,118 Posts
    Hi,

    I have a userfield called "workregion" - it contains a text string - lets say "London" - In my Login Update Profile form, I have a <select> element for the "workregion_01" field, which includes "London" as one of its values - question is.... how can I make the <select> field show that "London" is the currently selected value on form load....?

    Code for the <select> element on the Login Update Profile page is:

    <select name="workregion_01">
    <option value="" [[!+reg.workregion_01=``]]>Region 01 - Please select...</option>
    <option value="SouthWest" [[!+reg.workregion_01:is=`SouthWest`:then=`selected`]]>SouthWest</option>
    <option value="London" [[!+reg.workregion_01:is=`London`:then=`selected`]]>London</option>
    <option value="Wales" [[!+reg.workregion_01:is=`Wales`:then=`selected`]]>Wales</option>
    <option value="Scotland" [[!+reg.workregion_01:is=`Scotland`:then=`selected`]]>Scotland</option>
    </select>
    


    So, I need a way of making the "London" option include the value "selected"....

    Any ideas?????


    cheers,

    dubbs.

    This question has been answered by dubbs. See the first response.

      • 44580
      • 189 Posts
      Just thinking out loud here...

      If you haven't already, store these values in a table. You could then use two migxloopcollections to get first the default value from your user table and then get all the rest except the default from the workregion table. The tpls for each collection would be the same and would build your option list.
        • 8168
        • 1,118 Posts
        Quote from: gissirob at Sep 28, 2015, 04:59 PM
        Just thinking out loud here...

        If you haven't already, store these values in a table. You could then use two migxloopcollections to get first the default value from your user table and then get all the rest except the default from the workregion table. The tpls for each collection would be the same and would build your option list.

        thanks - not using MIGX before... Any chance of a code example? Any other ways this is possible guys?
          • 37105
          • 194 Posts
          In the Login scripts &useExtended property is set by default so extended user fields can also be called by +fieldname. So does this work?

          <select name="workregion_01">
          <option value="" [[+workregion=``]]>Region 01 - Please select...</option>
          <option value="SouthWest" [[+workregion:is=`SouthWest`:then=`selected`]]>SouthWest</option>
          <option value="London" [[+workregion:is=`London`:then=`selected`]]>London</option>
          <option value="Wales" [[+workregion:is=`Wales`:then=`selected`]]>Wales</option>
          <option value="Scotland" [[+workregion:is=`Scotland`:then=`selected`]]>Scotland</option>
          </select>
            Codeplaza Webdesign: for professional websites at low cost
            • 44580
            • 189 Posts
            If you've not used migx then it could also be done as a snippet. Maybe something like this (not tested):
            $current_id = $modx->user->get('id');
            
            $sql = "SELECT userworkregion as workregion from user_ext_table WHERE user_id = $current_id";
            $sql .= "UNION SELECT workregion as workregion from workregion_table";
            $sql .= "WHERE workregion NOT IN (SELECT userworkregion from user_ext_table WHERE user_id = $current_id)";
            
            foreach ($modx->query($sql) as $row) {
            $output = $modx->getChunk('displayWRChunk',array(
               'wregion' => $row,
            ));
            }
            return $output;

            (I'm assuming the userworkregion is in an extended table rather than moduser).

            And the displayWRChunk chunk would be:
            <option value="[[+wregion]]" [[!+reg.workregion_01:is=`[[+wregion]]`:then=`selected`]]>[[+wregion]]</option>

            All the work is done by the sql. The current value is the top of the list and the rest are below the dropdown. You could even add an ORDER BY to the second query if you want the rest of the selections to be alphabetical.

            I'm sure there are better ways, and people like Bob Ray should be able to point these out - I am not really a coder.
              • 3749
              • 24,544 Posts
              You may have a parsing order issue where the reg.workregion.. placeholders are not set at the time the conditional is parsed. First, try removing the ! from the placeholder tag. That ! delays the processing of the tag, possibly until it's to late.

              I usually use a snippet and do it this way. It avoids the use of conditional output modifiers so it's faster and more reliable.

              <option value="[[+wregion]]" [[+reg.workregion_01_selected]]"</option>


              Then in the snippet:

              /* If workregion_01 is set */
              $modx->setPlaceholder('reg.workregion_01_selected', 'selected="selected"');
              
              /* else */
              $modx->setPlaceholder('reg.workregion_01_selected', '');



                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
              • discuss.answer
                • 8168
                • 1,118 Posts
                Quote from: BobRay at Sep 29, 2015, 04:00 AM
                You may have a parsing order issue where the reg.workregion.. placeholders are not set at the time the conditional is parsed. First, try removing the ! from the placeholder tag. That ! delays the processing of the tag, possibly until it's to late.

                I usually use a snippet and do it this way. It avoids the use of conditional output modifiers so it's faster and more reliable.

                <option value="[[+wregion]]" [[+reg.workregion_01_selected]]"<="" option="">


                Then in the snippet:

                /* If workregion_01 is set */
                $modx->setPlaceholder('reg.workregion_01_selected', 'selected="selected"');
                
                /* else */
                $modx->setPlaceholder('reg.workregion_01_selected', '');



                </option>

                Thanks Bob - no need for any additional code. I can confirm this works on the <select> option elements:

                <option value="SouthWest" [[+workregion_01:is=`SouthWest`:then=`selected`]]>SouthWest</option>
                
                  • 3749
                  • 24,544 Posts
                  I'm glad you got it sorted. smiley
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting