We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40541
    • 85 Posts
    Have been doing some digging around in how I would delete a record and beleive the following snippet would do the job

    $deleteRow = $modx->myDB->getObject('LogData',array('callid' => $callid));
    
    if ($deleteRow->remove() == false) {
       echo 'An error occurred while trying to remove the row!';
    }
    


    I am trying to call this like so:

    <button type="button" class="btn btn-danger" onclick="[[!dbrowdelete? &callid=`[[+callid]]`]]">Delete</button>


    Now this doesn't work, I believe, because MODX is interpreting the tags before the onclick call.

    I have found bearly anything using onclick to call a snippet, which I thought was strange as I imagine something like this would be quite common - then again I am probably doing it completly wrong?

    The only reference I have been able to find is that you need AJAX to do this, which would lead into a world of hurt for me to try and get it to work. The only reference I have found of integrating MODX with AJAX is but I'm not sure that is the right road for the simple thing I need to do?

    Many thanks in advance.

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

    • discuss.answer
      • 4172
      • 5,888 Posts
      you cannot run a snippet client-side just by a javascript-click-event.

      you will need to call another resource by an ajax-request, where you have placed your snippet in that resource.
      you can also have your button as a link to the current resource

      <a href="[[~[[*id]]? &callid=`[[+callid]]`]]">


      and check for $_GET['callid'] in your snippet.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 33968
        • 863 Posts
        MODX tags are parsed server-side before you even receive any html at all.

        You could do this simply by placing your 'dbrowdelete' snippet in a separate resource with a blank template, then execute an ajax request to that resource when the button's click event is triggered.

        Example using jQuery (untested):
        <button type="button" class="btn btn-danger" data-callid="[[+callid]]">Delete</button>
        <script>
        $('button').on('click', function() {
            var callid = $(this).data('callid');
            $.ajax({
                url: "[[~7]]"  // let's say your ajax resource id is '7'
                ,data: { callid: callid }
                ,type: 'post'
                ,success: function(data) {
                    // do something if successful
                }
            });
        });
        </script>
        

        'callid' will be posted to your ajax resource, so you'll need to sanitise it and do whatever security checks are necessary before passing it to your existing 'dbrowdelete' snippet code.

        If you want to send back a success/failure response perhaps with some data, have your snippet encode it to json and return it.

        There are some forum posts around about how to implement a more robust ajax connector in conjunction with $modx->runProcessor(). Definitely worth having a look, especially if you need additional ajax functionality.
          • 40541
          • 85 Posts
          Thanks for posting, both answers helped. Tagged $_GET as the answer as this was best for me. Cheers.