We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47166
    • 9 Posts
    Hello Community,

    maybe I am thinking a little bit too complicated, or are totally lost in my thoughts, but I cannot find any solution for the problem I am facing.

    The situation is: I am having a list containing several database entries. For this purpose I am having a single chunk "listEntryTpl" containing something like
    <b>Name:</b> [[+entry_name]]<br />

    This chunk is called through a snippet "showList" for every database entry. This snippet itself is called in a page-resource. When I have a look at this page-resource everything works fine.

    Now I want to have an "edit link" next to entry; so the result should look like "Name: Test-Entry - Edit", while "Edit" should be a link to an edit form.

    How can I achieve this? I already tried something with a "sub"-chunk "listEntryEditTpl", containing
    <a href="[[~7? &id=[[!+id]]]]">Edit</a>
    , which displays a link to another resource, that shall display the edit form's chunk. The entry's id is passed well to the link, but how to resolve it in the new resource?

    I am absoultely sure, there is a quit simple way to achieve what I am doing.

    Thank you in advance.

    Dennis
    • If I understand what you are doing, I would use AJAX to popup an overlay with the edit form, and again use AJAX to handle the form submission. However, one way to do it your way...

      Resource A has the list. Each item in the list has an "edit" button so you can edit it. This edit button should go to page #7, with a form for editing the list item. The URL for this form page looks like "edit.html?id=42", where 42 is the list item selected. What you need is to be able to have your form know that it's working with item #42.

      There are a few ways to do this.

      You can make each button a miniature form with a hidden field to hold the value, and clicking the button submits the form to page #7, so you can use POST instead of GET. You would need to validate that POST value before using it for anything.

      Once you're in page #7, you can use a small snippet to fetch the $_GET or $_POST, and make a placeholder out of it. In your form, have a hidden field that uses that placeholder as its value. Or skip the placeholder, and just use the snippet call as the value of the hidden field. Make sure to validate the $_GET/$_POST value in your snippet so that somebody doesn't try to pass something nasty in it.

      Or instead of a snippet you can install pdoTools and enable its custom parser, or install FastField (which the pdoTools custom parser is based on). This gives you a new MODX tag, [[#...]]. It includes the ability to get any field from any resource, as well as the PHP arrays such as GET and POST.
      <input type="hidden" name="itemID" value="[[#get.id]]">

      The major disadvantage to this is that the GET value is not validated in any way, so you would need to create a custom output modifier to make sure it's not anything dangerous - it should just be in integer, nothing more. A snippet like this named 'isnum' would do the job (remember, all GET and POST values are strings!)
      <?php
      $output = is_numeric($input) ? $input : '0';
      return $output;

      Now your fastfield tag would look like this:
      <input type="hidden" name="itemID" value="[[#get.id:isnum]]">


      You should still do further validation of the incoming itemID value in your form processing before you go anywhere near the database or the filesystem with it. Never, ever forget that any data coming from any user's browser is very dangerous.
      [ed. note: sottwell last edited this post 9 years, 9 months ago.]
        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
        • 47166
        • 9 Posts
        Hey scottwell,

        thank you very much for your quick reply.

        Well, I know, having it all in a way like "http://mydomain.com/example.html?id=42" is quite dangerous and I totally agree with you, that this is not the best way to handle it - escpecially due to the fact, the site will be visited quite frequently.

        I would also prefer more the solution using the AJAX overlay to show the edit form and process it.
        But, how can I achieve this?
        At the moment I can only think on having it displayed in an AJAX window as a rendered HTML page, what would be like displaying "example.html?id=42" inside; so, you can see, I would not have any benefit. sad

        Please give me some more advice.

        Thank you in advance.

        Dennis
        • There are dozens of tutorials and JQuery plugins to do this. For example,
          http://hayageek.com/examples/jquery/ajax-form-submit/index.php
          http://jqueryui.com/dialog/#modal-form
            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
            • 47166
            • 9 Posts
            Hello sottwell,

            thank you for your reply.

            The thing is, I know how to display a modal window. The point I am unsure about is the program logic.
            I am quite new to MODx development and I want to be sure that my aproach is not too complicated or points in a totally wrong direction.

            What I am having right now is

            • a chunk for displaying the create/edit form.
            • a snippet that loads the mentioned chunk and creates a new record.
            • a snippet that loads the mentioned chunk and data from a table (record is identified by a snippet parameter), fills the form and updates a record.

            Actually I get into the edit form via http://example.com/edit.html?id=42 - as mentioned before, this is not very safe and at least not very pretty.

            What I would do now is the following:

            • When the user clicks an "edit" link, a JS function is called to display a window.
            • This window renders the edit page via AJAX into itself.
            • The record's Id is passed via a JSON envelope in the AJAX render call which is posted to the page to be renderd.
            • In the page to render a snippet is called, retrieving the record by the posted JSON envelope and populating the edit form, which comes out of the chunk.
            • After clicking "Ok" the record is saved, the window gets closed, the parent page is refreshed using a new AJAX call.
            Hmmm... It looks like always... Sometimes it seems to make sense, to get a little distance between the problem and thoughts about a possible solution. wink

            But could you please tell me, if this aproach does make sens to solve the problem, or do I have a big leak in my thoughts?

            Thanks in advance.

            Dennis