Simple Ajax Login Tutorial
2015-06-24 00:00
786 查看
摘要: 利用php/ajax 简单的登陆界面
Ajax (an acronym for asynchronous JavaScript and XML) is a group of interrelated web development techniques used on the client-side to create asynchronous web applications. With Ajax, web applications can send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page.
in this post I will explain (line by line code commentary) how to create a simple ajax login page using JQuery and PHP
the Form (ajax submit) index.php
the JQuery Code (ajax processor) functions.js
the PHP Code (php processor) login.php
the Private Area (to check if the code is work or not) private.php
Ajax (an acronym for asynchronous JavaScript and XML) is a group of interrelated web development techniques used on the client-side to create asynchronous web applications. With Ajax, web applications can send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page.
in this post I will explain (line by line code commentary) how to create a simple ajax login page using JQuery and PHP
the Form (ajax submit) index.php
<form method="POST"><!-- We can use GET also as submitting method--> <label>Username</label> <input id="username" name="username" type="text"><!-- the username input --> <label>Password</label> <input id="password" name="pasword" type="password"><!-- the password input --> <input id="submit_login" name="submit" value="Login" type="submit"><!-- submit button --> <span class="errormess"></span><!-- the ajax result wrapper --> </form>
the JQuery Code (ajax processor) functions.js
$(function() { $("#submit_login").click(function() { // if submit button is clicked var username = $("input#username").val(); // define username variable if (username == "") { // if username variable is empty $('.errormess').html('Please Insert Your Username'); // printing error message return false; // stop the script } var password = $("input#password").val(); // define password variable if (password == "") { // if password variable is empty $('.errormess').html('Please Insert Your Password'); // printing error message return false; // stop the script } $.ajax({ // JQuery ajax function type: "POST", // Submitting Method url: 'login.php', // PHP processor data: 'username='+ username + '&password=' + password, // the data that will be sent to php processor dataType: "html", // type of returned data success: function(data) { // if ajax function results success if (data == 0) { // if the returned data equal 0 $('.errormess').html('Wrong Login Data'); // print error message } else { // if the reurned data not equal 0 $('.errormess').html('<b style="color: green;">you are logged. wait for redirection</b>');// print success message document.location.href = 'private.php'; // redirect to the private area } } }); return false; }); });
the PHP Code (php processor) login.php
session_start(); // define a username and password for test you can change this to a query from database or anything else that fetch a username and password $username = 'dongchao'; $password = '123456'; if (isset($_POST)) { // if ajax request submitted $post_username = $_POST['username'] ; // the ajax post username $post_password = $_POST['password'] ; // the ajax post password if ($username == $post_username && $password == $post_password) { // if the username and password are right $_SESSION['username'] = $post_username; // define a session variable echo $post_username; // return a value for the ajax query } }
the Private Area (to check if the code is work or not) private.php
session_start(); if (!isset($_SESSION['username'] )) { // if the session variable is not exists echo 'You are not logged. <a href="index.php">login</a>'; // print error message } else { // if the session variable is exists echo 'hello <b>'.$_SESSION['username'] .'</b> you are logged in. <a href="logout.php">logout</a>'; // print welcome message }
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- 解决Ajax悬停效果,无法遮蔽FLASH的问题
- 再谈Jquery Ajax方法传递到action(补充)
- Dom在ajax技术中的作用说明
- PHP+Apache在Windows 9x下的安装和配置
- 使用Ajax实时检测"用户名、邮箱等"是否已经存在
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- 探讨Ajax中同步与异步之间的区别