We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40097
    • 19 Posts
    I know this one is easy but I think my hang up is in not knowing what is suppose to go where...
    I have a grid of products on a site, each feature a button that is a url to a form. The URL is something like page.html?opt=2
    opt being the number of the product or option that they selected..
    On the Form I want a dropdown to be already selected based on the product they chose.
    I tried this html/PHP combo but no go.. The php was still in the rendered code.

    <select name=“SelectedPackage” class="select-opt">
    	<option value="">Select an option...</option>
    	<option value="proHost" <?php if ($_GET["opt"] == 1) { echo " selected"; } ?>>Package 1</option>
    	<option value="proHost" <?php if ($_GET["opt"] == 2) { echo " selected"; } ?>>Package 2</option>
    </select>
    


    My guess is that I need to create a snippet and put all of this in there but I'm not 100% the best way.
    Thanks for your assistance and guidance.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You could use fastField here (or install pdoTools with its custom parser that does the same thing)...
      <option value="proHost" [[#get.opt:is=`1`:then=`"selected"`]]>Package 1</option>
      <option value="proHost" [[#get.opt:is=`2`:then=`"selected"`]]>Package 2</option>
      

      http://rtfm.modx.com/extras/revo/fastfield

      Otherwise, a simple snippet to get the GET value and return the "selected" string would work in the same way.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        I've done something similar with a snippet and placeholder:

        [[!GetSelected]]
        ...
        <option value="proHost" [[+selected1]]>Package 1</option>
        <option value="proHost" [[+selected2]]>Package 2</option>
        




        <?php
        /* GetSelected snippet */
        $option = $_GET['opt'];
        $modx->setPlaceholder('selected' . $option, 'selected="selected"');
        return '';
        


        The appropriate placeholder will be set and the other placeholder tags will be removed by the parser. The snippet tag can go anywhere as long as it's above the placeholders. [ed. note: BobRay last edited this post 12 years, 1 month ago.]
          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
          • 40097
          • 19 Posts
          Thank you so much for the replies.
          @BobRay,I like you solutions best. I'm already use FormIT for the form so I really don't want to add another plugin at this time
          FWIW I noticed you have dbl quotes around selected, once I removed those the code worked perfectly.
          This is concept has also educated me on how maybe I could use the same concept with other applications.
          Thanks again! This community rocks.


            • 3749
            • 24,544 Posts
            I'm glad I could help. In terms of HTML validity, I believe the actual value should be as follows ( could be wrong), though I think most, if not all, modern browsers will accept it the way you did it.

            $modx->setPlaceholder('selected' . $option, 'selected="selected"');


            My code failed because I split the difference used the quoted string but not the attribute name and equals sign.

            [Update]
            I just checked with the validator. Either style is acceptable unless you're using an XML doctype. [ed. note: BobRay last edited this post 12 years, 1 month ago.]
              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