Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 8. Services with a Smile > Time for action — the AJAX login form

Time for action — the AJAX login form

Add the following lines to the following files:

  1. To the bottom of the sites/all/themes/dpk_mobile/templates/page.tpl.php file, add the following lines:

    <?php if ($user->uid == 0) { ?>
    <div data-role="page" id="login" data-theme="<?php print $page_data_theme ?>">
    <header data-role="header" data-position-"inline">
    <h1>User Login</h1>
    </header>
    <div data-role="content" data-theme="<?php print $page_data_theme ?>">
    <?php $form = drupal_get_form("user_login"); $form['#attributes']['data-ajax'] = "false"; echo drupal_render($form); ?>
    <div class="ui-body ui-body-e" style="display:none;" id="form-errors"></div>
    </div>
    </div>
    <?php } ?>
    
    
    					  
  2. Edit the sites/all/themes/dpk_mobile/js/global.js file by adding the following lines:

    Drupal.behaviors.jQMPageInit = {
    attach: function(context) {
    $("#user-login").not(".jQMPageInit-processed").submit(function(evt){
    if (evt) { evt.preventDefault(); }
    toSubmit = {
    "username": $(this).find("#edit-name").val(),
    "password": $(this).find("#edit-pass").val()
    }
    $.ajax({
    url: "/rest/user/login.json",
    dataType: "json",
    data: toSubmit,
    type: "post",
    error: function(jqXHR, textStatus, errorThrown) {
    $("#form-errors").html("There was an error logging you in").show();
    },
    complete: function(jqXHR, textStatus){
    document.location.href = "/";
    }
    });
    }).addClass("jQMPageInit-processed");
    $("#user-logout").not(".jQMPageInit-processed").click(function(evt) {
    if (evt) { evt.preventDefault(); }
    $.mobile.showPageLoadingMsg();
    document.location.href = "/user/logout";
    }).addClass("jQMPageInit-processed");
    },
    detach: function(context){
    }
    }
    
    
    					  
  3. Navigate to http://m.dpk.local. The login/logout process should work as expected.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint