We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    I'm almost there, creating an ajax login for a bootstrap modal but have run into a little problem.

    Consider this:

    Here is the modal, stock bootstrap & stock login form:

    <div class="modal fade" id="LoginModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-body">
    
    <div id="loginMessage"></div>
            <div class="loginForm">
        <div class="loginMessage">[[+errors]]</div>
        <div class="loginLogin">
            <form id="loginform" class="loginLoginForm" action="[[~[[*id]]]]" method="post">
                     <legend class="loginLegend">[[+actionMsg]]</legend>
                    <label class="loginUsernameLabel">[[%login.username]]
                        <input class="loginUsername" type="text" name="username" />
                    </label>
                    <label class="loginPasswordLabel">[[%login.password]]
                        <input class="loginPassword" type="password" name="password" />
                    </label>
                    <input class="returnUrl" type="hidden" name="returnUrl" value="[[+request_uri]]" />
                    [[+login.recaptcha_html]]
                    <input class="loginLoginValue" type="hidden" name="service" value="login" />
                    <span class="loginLoginButton"><button type="button" id="loginSubmit" name="submit">login</button></span>
             </form>
        </div>
    </div>
    <a href="[[~15]]">Forgot your Password?</a>
    </div>
    
        </div>
      </div>
    </div>



    Now the jquery:
    <script>
    
    	// ajax login
    	$(function() {
    
    		$("button#loginSubmit").click(function(){
    
    			console.log("running processor");
    
    			$.ajax({
    				type: "POST",
    				url: "[[~17]]",
    				dataType  : "json",
    				cache : false,
    				data: $('form#loginform').serialize(),
    				success: function(data){
    
    					if(data.status == 'success'){
    
    						console.log("login success");
    
    						$("#loginMessage").html('success' + data.msg)
    						$("#LoginModal").modal('hide'); 
    						window.location.replace('[[~14]]');
    
    					}else if(data.status == 'error'){
    
    						console.log("login error");
    
    						$("#loginMessage").html('err' + data.msg)
    
    					}
    
    				},
    
    				error: function(data){
    					//alert("failure");
    				}
    
    			});
    
    		});
    
    	});
    
    </script>



    Now the processor:

    <?php
    
    $status = 'success';
    $msg = 'login was successful';
    
    $output = $modx->runSnippet('Login',array(
       'errTpl' => 'ajaxLoginErrorTpl',
       'loginResourceId' => '14',
       'logoutResourceId' => '15',
    ));
    
    $loginerror = $modx->getPlaceholder('errors');
    
    if(isset($loginerror)){
    
    	$status = 'error';
    	$msg = $loginerror;
    
    }
    
    $response_array = array(
    	'status' => $status,
    	'msg' => $msg,
    	);
    
    header('Content-type: application/json');
    
    $output = json_encode($response_array);
    
    return $output;






    This works fine for a failed login, the errors get caught and displayed in the loginMessage div.

    The problem starts when a user has a ~successful~ log in. A user has to click twice to "appear" logged in.

    I saw only "appear" logged in because on the first click the modal does not disappear & the user is not redirected BUT the user does get logged in. Login is trying to redirect, the response array is not returned and the jquery does not complete. On the second click the user does get redirected, I don't know why.

    I can ~sort of~ fix this by altering the login.php package ~ adding a return to the redirectAfterLogin function before it can redirect, but we all know that is dirty pool.


    So;

    - Is there a way to tell login to not redirect on a successful login?

    if not,

    - Any thoughts on how to fix this?



      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 4172
      • 5,888 Posts
      google for 'modx ajax login'

      This is the first result.
      http://forums.modx.com/thread/44831/tutorial-making-jquery-ui-based-ajax-login

      Looks good to me.

      Seems the redirect-resource should return something, what is usefull for your js.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 26503
        • 620 Posts
        I did find that before posting ~ problem is that it's not using a modal and more importantly ~ it still redirects.

        I need to convince login to ~not~ redirect after a successful login so I can handle that in my javascript.


        Hmmmmmm.... think I just found a solution. Going to try using a posthook to return some success conditions....



        Quote from: Bruno17 at Jun 21, 2014, 06:54 PM
        google for 'modx ajax login'

        This is the first result.
        http://forums.modx.com/thread/44831/tutorial-making-jquery-ui-based-ajax-login

        Looks good to me.

        Seems the redirect-resource should return something, what is usefull for your js.
          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525
          • 32507
          • 142 Posts
          Hi Sean,

          I'm trying to implement similar to my site. Where to put processor PHP?
          • Should be in a snippet, in a resource with no template.
              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
              • 32507
              • 142 Posts
              I still don't get this processor? I've created new resource & snippet. Insert snippet to this resource (empty template). Then what?
              [ed. note: wintertribe last edited this post 8 years, 11 months ago.]
                • 26503
                • 620 Posts
                What do you mean "don't get" - is something not working? errors?

                Quote from: wintertribe at May 07, 2015, 09:21 AM
                I still don't get this processor? I've created new resource & snippet. Insert snippet to this resource (empty template). Then what?
                  *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                  Sean Kimball CLP, CLS.
                  Technical Director / Sr. Developer | BigBlock Studios
                  ._______________________________________________.
                  Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                  27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                  phone/fax: 905-426-5525
                  • 32507
                  • 142 Posts
                  I like to have customer login in Bootstrap Modal. Login modal should be accessible all pages on site (link in header section). At this point Login seems to work, but i don't get any LoginMessage. (etc)

                  What i mean with "don't get" is that i don't fully understand how to implement php processor.
                  I don't fully "get" sottwell. I made snippet and tagged it to resource?

                  Should be in a snippet, in a resource with no template.

                  Also in jquery contains modx tags, so i guess it cannot be external file?

                  I don't have strong coding background and this seems to be implemented on a way which is new to me.

                  I'm a bit "slow" these days sad
                    • 26503
                    • 620 Posts

                    The javascript does use a modx tag that needs to be processed, but the script actually does not need to do that - you could hard code the urls in there and include the script file if you like.

                    as for the snippet call, the javascript cannot call a snippet directly from modx, but has to request a resource which then in turn can include the snippet to be executed.

                    The reason the snippet needs to be in a modx resource is because it is running another modx snippet [login] & needs access to the modx object to do that.


                    You could write a standalone php connector that included the modx object as well and forgo the extra processor -> resource -> snippet roundabout as well.





                    Quote from: wintertribe at May 08, 2015, 04:50 AM
                    I like to have customer login in Bootstrap Modal. Login modal should be accessible all pages on site (link in header section). At this point Login seems to work, but i don't get any LoginMessage. (etc)

                    What i mean with "don't get" is that i don't fully understand how to implement php processor.
                    I don't fully "get" sottwell. I made snippet and tagged it to resource?

                    Should be in a snippet, in a resource with no template.

                    Also in jquery contains modx tags, so i guess it cannot be external file?

                    I don't have strong coding background and this seems to be implemented on a way which is new to me.

                    I'm a bit "slow" these days sad
                      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                      Sean Kimball CLP, CLS.
                      Technical Director / Sr. Developer | BigBlock Studios
                      ._______________________________________________.
                      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                      phone/fax: 905-426-5525