We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42082
    • 72 Posts
    Hey yall me again,
    I was just wondering how would I use ajax with the Login snippet? I have read lots on ajax and so I have decided to use jquery's easy ajax. So hear is what I have so far But i don't know where to go from here.
    <script>
        $(document).ready(function(){
            $('.loginLoginForm').submit(function(){
                var formData=$(this).serialize();
                $.post('[[~109]]',formData);
                return false;
            })
        })
    </script>


    I don't know very much about the Login snippet but I hope some of you guys can help me out with that part But basically what this code is doing is: A.Make sure all html on the page is loaded. B. when the user submits the Login Form with a class of loginLoginForm create a variable with all the fields and values and then use ajax to send that variable the resource 109 which has has this code in it:
    [[!Login &tplType=`modChunk`]]

    And at the end is says return false which just means don't complete the normal function of the click.This is placed so that the form wont go to another page. Now This is what I have so far And I think the next step is to put a call to a function on the end of the jquery ajax string to handle the response from the server. But I have no idea how to process the response from the login snippet. And help would be very great.

    Thanks in advance
    ModxNooby

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

      • 3749
      • 24,544 Posts
      You're on the right track, but I don't see why you want to use Ajax for this. You're going to forward to another page that handles the data, but the form's action attribute is going to do that anyway and all the form variables are going to be in the $_POST array, which appears to be what you want.

      If you're heading toward using JS validation of the form before it's submitted, you can do that in your JS without any Ajax. If the form is invalid, pop up the necessary alerts and return false so the form won't be submitted. If it's valid, just return true.

      A snippet on the receiving page (or on the same page if the form posts to itself) can handle the data from the $_POST array, again with no Ajax and I think it's easier to make things secure that way.

      Maybe I'm missing something.



        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
        • 42082
        • 72 Posts
        So the thing is bob, I need to use ajax or something similar because I am using a plugin for jquery called mb.extruder. more info here: http://pupunzi.com/#mb.components/mb.extruder/extruder.html. So i am putting a login on the extruder positioned on the right. So what I want is to have users login and the extruder load up there profiles onto the extruder without a page reload. So user clicks on the tab labeled login. The extruder opens up the user logs in and there profile loads up as soon as they hit the login button on the forum. So that is why i am using ajax.
          • 3749
          • 24,544 Posts
          That makes sense. There won't be any $_POST data until after the form is actually submitted. You'll can use JQuery to grab the field values based on the ID of the input field. Then you can to use an Ajax call to get the user's profile by sending it the username and password, return the User Profile as JSON, and use JQuery to fill in the extruder fields.
            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
            • 42082
            • 72 Posts
            could you please show me in code how you might do this? are you saying i should send the ajax to the login snippet php or to my own coded php script? and if my own what would that look like?
              • 3749
              • 24,544 Posts
              It would be your own snippet and you'd put a tag for it in a resource with an empty template. The Ajax call would be made to the URL of that resource. The snippet would accept the username from the Ajax call, get the user profile and return that as a JSON string. Maybe something like this:

              $userId = $_POST['userId'];
              
              $selectedUser = $modx->getObject('modUser', $userId);
              $profile = $selectedUser->getOne('Profile');
              if ($profile) {
                  $fields = $profile->toArray();
                  return $modx->toJSON($fields);
              } else {
                  return $modx->toJSON(array('Error' => 'No Profile');
              }


              As for putting the response into the extruder fields -- I have no idea. wink

                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
                • 42082
                • 72 Posts
                But bob I cannot see how this code would work. To my understanding of this code(please correct me if i am wrong) this is just taking the username submitted and grabbing the profile for it. But what about handling the password? I don't quite understand how this would work. Unless this code will some how be called by the login snippet after it has confirmed the username and password are correct. Please tell me if this is right or if i am way off.

                Thanks, modxnooby
                  • 3749
                  • 24,544 Posts
                  The way I'm thinking of it, your JS code can already have the password, since the user has already typed it into the form, which is in the browser's memory and available via JavaScript.
                    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
                    • 42082
                    • 72 Posts
                    So how if i might ask would the code above be able to be blended with javascript to be able to get that functionality?
                      • 42082
                      • 72 Posts
                      And thanks so much for your help in this matter. It has been very helpful