We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37523
    • 141 Posts
    Is it possible for a snippet to be run once a user clicks a link (a href=""). What I am trying to do is as follows:

    - have a list of users
    - beside each user is a button to delete or edit that particular user. This would be based on there id
    -Once a button beside a user in the list is clicked, say delete, the id of the clicked user will be passed to a context setting and then a new page will appear offering a "Are you sure you wish to delete the user with ID?". It is this process that i would like to work via a snippet.
    - once another button is pressed the user will be deleted.

    The user id in the above would be accessed within a global context setting.

    Any advice would be appreciated?
      Glyn Szasz
      Sydney, Australia

      Happy to help (when can) and happy to learn

      If you are a Sydney based MODX developer would love to hear from you. Please contact me.
      • 3749
      • 24,544 Posts
      It's certainly possible to do it the way you're describing it, although normally, each button would be part of a mini-form with a hidden "id" field that uses the "post" method to send the ID to the receiving page with another form and snippet on it which would do the confirmation and delete the user.

      That said, the usual way to do that these days is with Ajax and JavaScript, which would allow the whole operation to take place without the user leaving the original page and without the page reloading.

      You make it so the button click triggers some JavaScript that pops up an "Are you sure?" window. If the user says yes, an Ajax request is made to a processor file that deletes the user. It sounds a lot more complicated than it is. In JQuery, the JS would look something like this:



      $('.removeUser').click(function (event) {
          if (confirm('Are you sure you want to delete this user?')) {
              $.ajax({
                  url: 'myUrl',  /* Url of the remove-user processor */
                  type: "POST",
                  data: {
                      /* data stuff here -- ID of the user */
                  },
                  success: function () {
                      /* does some stuff here like delete the user from the on-screen list */
                  }
              });
          }
      });
      



      No matter how you do it, you're biggest issue is going to be making it secure so that unauthorized people can't just delete your users at will.

        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