We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 31765
    • 6 Posts
    Hi, I’m new to modx so forgive me if my question sounds really basic. I am more used to things like Smarty and am wondering how I can translate this into using modx.

    I need a registration form (I will store this info in another database besides the modx db, I think I can handle this part).

    I’ve been told the best way to go about it is to use code snippets that then call chunks for the HTML. But the part I don’t get is how to separate content from logic... the form tag should be like

    <form method="POST" action="<?=$PHP_SE:LF?>">

    or something similar. How do I get the document name in a chunk?

    Also, the "sticky" values of the form elements...

    <input type="text" name="last_name" value="<?=$_POST[’last_name’]?>" />

    how would i do something like that in a chunk? Maybe template variables are supposed to come in here somewhere.

    Whether I’m way or just a little off, I’d appreciate you pointing me in the right direction.

    Thanks in advance.
      • 33337
      • 3,975 Posts
      Check the great eForm snippet..!
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 21257 MODX Staff
        • 730 Posts
        In a chunk, you can make a link to the current document like this
        <form method="POST" action="[~[*id*]~]">
        

        caveat: it won’t work if you are running the PHx plugin

        Other, custom, template variables that you have assigned to your template can be written into a chunk like this:
        <p>The value of my variable is [*myCustomVariable*].</p>
        


        Aside from template variables, there is a way to write other values from a snippet’s php code into chunks. Placeholders.
        If you have your own snippet, you set a placeholder like this:
        $modx->setPlaceholder('priceOfRaisins', '$100,000');
        


        Then, in any chunk that comes after that snippet, you can do this:
        <p>You can have some raisins, but you will have to give me [+priceOfRaisins+] first.</p>
        

          Mike Schell
          Lead Developer, MODX Cloud
          Email: [email protected]
          GitHub: https://github.com/netProphET/
          Twitter: @mkschell
          • 31765
          • 6 Posts
          thanks for the help. But how would I make a select (drop-down) field sticky from inside a chunk?
            • 31765
            • 6 Posts
            i guess i could have a group of variables with the one corresponding to the selected one having the value "selected" and use it like this

            <select name="food">
            <option name="potato" [*potato_selected*] />
            <option name="tomato" [*tomato_selected*] />
            </select>

            ?? any better ideas?
              • 21257 MODX Staff
              • 730 Posts
              I used to do this by creating special placeholders in my snippet, but recently I’ve learned that PHx can make this much more elegant. (Note, above, where I recommended against using [~(*id*)~] as the "action" to your form when using PHx, maybe just replace (*id*) with the id of the document where your form is.)
              Then, install the PHx plugin and have a look at the PHx documentation http://wiki.modxcms.com/index.php/PHx.
              One example of how an html option can be coded using PHx, extending my previous example:
              <select name="priceOfRaisins">
              <option value="test1"[+priceOfRaisins:is=`test1`:then=` selected="selected"`+]>test1</option>
              <option value="test2"[+priceOfRaisins:is=`test2`:then=` selected="selected"`+]>test2</option>
              

              Caveat: I did not test this code, I hope the syntax is correct. And maybe there’s an even better way to do it. I’d love to see anyone’s variation or better use of PHx for this purpose.
                Mike Schell
                Lead Developer, MODX Cloud
                Email: [email protected]
                GitHub: https://github.com/netProphET/
                Twitter: @mkschell
                • 31765
                • 6 Posts
                thanks a lot... i’ll look into PHx.
                  • 31765
                  • 6 Posts
                  I have a snippet named regForm and a chunk named regForm, in the chunk named regForm, I have something like this...

                  <?php

                  if ($_SERVER[’REQUEST_METHOD’] == "POST") {
                  $modx->setPlaceholder(’first_name’, $_POST[’first_name’]);
                  $modx->setPlaceholder(’last_name’, $_POST[’last_name’]);
                  $modx->setPlaceholder(’house_number’, $_POST[’house_number’]);
                  $modx->setPlaceholder(’street1’, $_POST[’street1’]);
                  $modx->setPlaceholder(’street2’, $_POST[’street2’]);
                  }

                  $modx->getChunk(’regForm’);
                  $modx->putChunk(’regForm’);

                  ?>

                  ==

                  The chunk regForm is the HTML form.

                  I have this in the page that loading the snippet.

                  test test [[regForm]]

                  All I’m seeing is the "test test"... Any ideas?
                    • 21257 MODX Staff
                    • 730 Posts
                    At the end of a snippet, you have to return a value, like you do from a function.
                    I would try:
                    return $modx->getChunk('regForm');
                    

                    BTW, the core code says that putChunk is deprecated. I don’t think you’d need anything else.
                    Two other ways you could do it:
                    1) NOT return anything from the snippet, and put the Chunk call right in your template
                    2) set another placeholder with the value of the chunk (something like $modx->setPlaceholder(’form’, $modx->getChunk(’regForm’)); for example) This allows you to allow the snippet to decide what will be shown on the page - can be handy if you want to display a thank-you message or something, or even a preview page instead of always just the form chunk)
                      Mike Schell
                      Lead Developer, MODX Cloud
                      Email: [email protected]
                      GitHub: https://github.com/netProphET/
                      Twitter: @mkschell
                      • 31765
                      • 6 Posts
                      I tried your suggestion of

                      return $modx->getChunk('regForm');


                      but it’s not working... I also tried this...

                      return "first_name: " . $_POST['first_name'] . ' ' . $modx->getChunk('regForm');


                      It is displaying the POSTed variable, but the chunk isn’t getting there. I should note that my chunk and my snippet have the same name (but I assume that these are different namespaces and there’s no problem there.)

                      Any ideas why the chunk would be blank?