We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32098
    • 31 Posts
    This is almost certainly a dumb question but I can’t find anything relevant on the forums post-2005. I’m still trying to get the hang of writing PHP in a Modxey sort of way. I have written a few snippets and managed to get them running nicely from the standard snippet calls in double brackets. But how do you call a snippet from a link?

    Some detail: I have a snippet that controls a couple of chunks and fills a table with a list of entries from a database table. Next to each entry I want a link so that someone can click to delete the row in the database table. On a static site I would call a PHP script from the link like so:

    <a href="dbase_entry_delete.php?id=' . $row['id'] . '">Delete</a>


    What is the Modxey way of doing the same thing? I am assuming that calling a deleteSnippet in the chunk for the table rows like so won’t work:

    <a href="[[!deleteSnippet? &rowid=`[[+rowid]]`]]">Delete</a>



    Snippets seem to kick into action automatically rather than waiting for events like clicks on links (that’s my experience of trying to call them from the action="" part of HTML forms).

    It’s late. After midnight here. Maybe in the morning I will appreciate just how dumb the question is.

      • 36502 ☆ A M B ☆
      • 491 Posts
      May be I didn’t understand what you need...
      but can’t you call a modx page with the snippet inside?

      Suppose you have your snippet called inside the page with id=ID_target:

      <a href="[[~ID_target]]&rowid=[[+rowid]]">Delete</a>


      and in the target page you could get the value of "rowid" and then pass it to the snippet.
        TilliLab | MODX Ambassador
        website
        • 3749
        • 24,544 Posts
        You could also make it a form:

        <form action=[[~##]] method="post">
        <input type="hidden" name="[[+rowid]]" />
        <input type="submit" value="Delete" />
        </form>


        Where ## is the ID of the target page.

        Then put a snippet on the target page (which could also be the current page if you send along another $_POST variable to indicate a postback), that uses $_POST[’rowid’]] for the row.

        If you’re sending to the current page, it would be:

        <form action=[[~[[*id]]]] method="post">
          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
          • 32098
          • 31 Posts
          Pandro and Bob, thanks so much for your suggestions. The sun is up now and things do seem a heck of a lot clearer. About the idea of putting the snippet in the content area of a new page: I have a couple of pages like that but I guess I was hoping that there was a way to call a snippet without creating a snippet-only page. Bob, the form suggestion sounds brilliant. Even if I had stayed up all night racking my feeble brains it would never have occured to me to use a form to create the link/button. I’m going to give it a go now.

          Thanks again. It would be great to say something like: "You guys rock," but to some of us in Europe (perhaps the age is more important than the location) such phrases just don’t come readily to the lips. Nevertheless, the gratitude is sincere.

            • 7327
            • 195 Posts
            If you don’t like using forms, why don’t you tell the snippet to get the GET parameter value and do what you need to do based on a condition?

            For example:

            <a href="[[~[[*id]]]]&rowid=`[[+rowid]]`]]&action=delete">Delete</a>

            So the anchor above should have something like www.whatever.com/index.php?id=1&rowid=1&action=delete

            In your snippet, you can grab the action type like so:

            if ($_GET[’action’] == ’delete’) {
            //DO WHAT YOU NEED TO DO WITH $_GET[’rowid’];
            }
              • 32098
              • 31 Posts
              Thanks for the 3rd suggestion. I will also give that a go and see how it works (but is there 2 too many closing brackets in the example you gave?).

              I have spent an hour or so trying to figure out why neither the link method nor the form method were working for me just now. I did a little experiment with two versions of the link code and the code put in the action="" part of the form, like so:

              <form  class="button" action="[[~[[11]]]]" method="post">
              <input type="hidden" name="display" value="TRUE" />
              <input type="submit" value="View and Update" />
              </form>
              
              <form  class="button" action="[[~11]]" method="post">
              <input type="hidden" name="display" value="TRUE" />
              <input type="submit" value="View and Update" />
              </form>
              
              <a href="[[~[[11]]]]&action=view">View and Update</a>
              
              <a href="[[~11]]&action=view">View and Update</a>
              


              The first one (i.e. with 4 brackets on either site of the resource number) ends up linking back to the current resource (which in this case is not 11). Only the second one (i.e. with 2 brackets either side and the curly line snuggling up to the left hand side of the resource number) works for me at the moment and sends the visitor correctly to resource 11 where the database entries will be displayed for editing.

              To see what is going on I had a quick peek at the source viewable in the browser and the result for the code above was :
              <form  class="button" action="" method="post">
              <input type="hidden" name="display" value="TRUE" />
              <input type="submit" value="View and Update" />
              </form>
              <form  class="button" action="display-client-list.html" method="post">
              <input type="hidden" name="display" value="TRUE" />
              <input type="submit" value="View and Update" />
              </form>
              <a href="&action=view">View and Update</a>
              <a href="display-client-list.html&action=view">View and Update</a>
              


              Resource 11 has an alias of "display-client-list" and that is only showing up in the entries with 2 brackets either side of the resource number.

              Some people are congenitally inclined to blame themselves, and I am left thinking I must have set something up wrongly somewhere if my links don’t work when they have the full 8 brackets. However, I am managing to get my snippets and chunks working now (albeit on only 4 brackets), so I shouldn’t complain.

              Thanks again.
                • 36502 ☆ A M B ☆
                • 491 Posts
                Hi!
                why you use 4 brackets?

                if you write the resource number I think it’s enough to write:

                action="[[~11]]"


                if you use the placeholder instead of the number, I think you need some more brackets...

                action="[[~[[*id]]]]"

                  TilliLab | MODX Ambassador
                  website
                  • 7327
                  • 195 Posts
                  Quote from: pandro at Jan 14, 2011, 05:29 AM

                  Hi!
                  why you use 4 brackets?

                  if you write the resource number I think it’s enough to write:

                  action="[[~11]]"


                  if you use the placeholder instead of the number, I think you need some more brackets...

                  action="[[~[[*id]]]]"



                  If you need the resource ID to be non-static, meaning it refers to either the resource ID of THIS RESOURCE (the current resource doc where this will appear in), you use *, and if you used something like reflect, you use + to get the resource ID of THAT RESOURCE.
                    • 3749
                    • 24,544 Posts
                    Let me see if I can make the brackets thing a little clearer. It really helps in using MODx if you understand what’s happening with the tags.

                    This is a link tag: [[~12]] -- it will be replaced by the URL of resource 12.

                    This is a resource field/TV tag: [[*id]] -- it will be replaced by the ID of the current page.
                    Another one: [[*pagetitle]]: -- will be replaced by the Title of the current doc.
                    And another one: [[*TV6]] -- this one will be replaced by the rendered output of Tv6.

                    This is a resource field tag nested inside a link tag: [[~[[*id]]]] -- if it’s on page 12, the inner tag will be replaced by 12, which will make it look like the first tag above, which then gets replaced by the URL, so again the URL for page 12.

                    This is a placeholder tag: [[+placeholder]] -- it will be replaced by whatever value is set in code (e.g. by Wayfinder or getResources) for that placeholder. You’ll often see these used for resource fields in a Tpl chunk used by a snippet that accesses various pages.

                    This is a setting tag: [[++setting]] -- it will be replaced by the value of the named setting. User Settings override Context Settings, which override System Settings (though often, only the System Setting exists).

                    This is a snippet tag: [[snippetName]] -- it will be replaced by the output of the snippet code (if any).

                    This is a chunk tag: [[$chunkName]] -- it will be replaced by the contents of the chunk.
                      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