In the HEAD part of the page I’ve included the JQuery library, the modal box and the AJAX Login script. When the “LOGIN” link is clicked (id=”login_link”) the modal box is triggered and the login form is shown (initially it’s hidden).
Here’s the content of init.js (I will explain you how it works below): view plaincopy to clipboardprint?
1. // Preload Images 2. img1 = new Image(16, 16); 3. img1.src="images/spinner.gif"; 4. 5. img2 = new Image(220, 19); 6. img2.src="images/ajax-loader.gif"; 7. 8. // When DOM is ready 9. $(document).ready(function(){ 10. 11. // Launch MODAL BOX if the Login Link is clicked 12. $("#login_link").click(function(){ 13. $('#login_form').modal(); 14. }); 15. 16. // When the form is submitted 17. $("#status > form").submit(function(){ 18. 19. // Hide 'Submit' Button 20. $('#submit').hide(); 21. 22. // Show Gif Spinning Rotator 23. $('#ajax_loading').show(); 24. 25. // 'this' refers to the current submitted form 26. var str = $(this).serialize(); 27. 28. // -- Start AJAX Call -- 29. 30. $.ajax({ 31. type: "POST", 32. url: "do-login.php", // Send the login info to this page 33. data: str, 34. success: function(msg){ 35. 36. $("#status").ajaxComplete(function(event, request, settings){ 37. 38. // Show 'Submit' Button 39. $('#submit').show(); 40. 41. // Hide Gif Spinning Rotator 42. $('#ajax_loading').hide(); 43. 44. if(msg == 'OK') // LOGIN OK? 45. { 46. var login_response = '<div id="logged_in">' + 47. '<div style="width: 350px; float: left; margin-left: 70px;">' + 48. '<div style="width: 40px; float: left;">' + 49. '<img style="margin: 10px 0px 10px 0px;" align="absmiddle" src="images/ajax-loader.gif">' + 50. '</div>' + 51. '<div style="margin: 10px 0px 0px 10px; float: right; width: 300px;">'+ 52. "You are successfully logged in! <br /> Please wait while you're redirected...</div></div>"; 53. 54. $('a.modalCloseImg').hide(); 55. 56. $('#simplemodal-container').css("width","500px"); 57. $('#simplemodal-container').css("height","120px"); 58. 59. $(this).html(login_response); // Refers to 'status' 60. 61. // After 3 seconds redirect the 62. setTimeout('go_to_private_page()', 3000); 63. } 64. else // ERROR? 65. { 66. var login_response = msg; 67. $('#login_response').html(login_response); 68. } 69. 70. }); 71. 72. } 73. 74. }); 75. 76. // -- End AJAX Call -- 77. 78. return false; 79. 80. }); // end submit event 81. 82. }); 83. 84. function go_to_private_page() 85. { 86. window.location = 'private.php'; // Members Area 87. }
// Preload Images img1 = new Image(16, 16); img1.src="images/spinner.gif";
img2 = new Image(220, 19); img2.src="images/ajax-loader.gif";
// When DOM is ready $(document).ready(function(){
// Launch MODAL BOX if the Login Link is clicked $("#login_link").click(function(){ $('#login_form').modal(); });
// When the form is submitted $("#status > form").submit(function(){
// Hide 'Submit' Button $('#submit').hide();
// Show Gif Spinning Rotator $('#ajax_loading').show();
// 'this' refers to the current submitted form var str = $(this).serialize();
// -- Start AJAX Call --
$.ajax({ type: "POST", url: "do-login.php", // Send the login info to this page data: str, success: function(msg){
// Preload Images img1 = new Image(16, 16); img1.src="images/spinner.gif";
img2 = new Image(220, 19); img2.src="images/ajax-loader.gif";
When DOM is ready (all page elements are loaded, including graphics) the Modal Box is launched when the user clicks on the “Login” link: view plaincopy to clipboardprint?
1. // Launch MODAL BOX if the Login Link is clicked 2. $("#login_link").click(function(){ 3. $('#login_form').modal(); 4. });
// Launch MODAL BOX if the Login Link is clicked $("#login_link").click(function(){ $('#login_form').modal(); });
When the form is submitted, the “Submit” button is hidden and the GIF spinning rotator/loader is shown: view plaincopy to clipboardprint?
1. // When the form is submitted 2. $("#status > form").submit(function(){ 3. 4. // Hide 'Submit' Button 5. $('#submit').hide(); 6. 7. // Show Gif Spinning Rotator 8. $('#ajax_loading').show(); 9. 10. // .................. 11. });
// When the form is submitted $("#status > form").submit(function(){
// Hide 'Submit' Button $('#submit').hide();
// Show Gif Spinning Rotator $('#ajax_loading').show();
// .................. });
The form data is serialized: view plaincopy to clipboardprint?
1. // 'this' refers to the current submitted form 2. var str = $(this).serialize();
// 'this' refers to the current submitted form var str = $(this).serialize();
Further on, the data is sent to “do-login.php” using AJAX. If ‘OK’ is returned then a confirmation message will be shown to the user before redirecting him to the private page. Notice how I used JQuery’s css(name, value) function to change the width and height of the modal box: view plaincopy to clipboardprint?
This registers our CallServer function on the page. We are essentially passing the selected item of the Dropdown list to the CallServer function if you see the source code of the output page the CallServer function will appear as follows:online