Home  >  Article  >  Backend Development  >  How to use php+AJax+json to implement login verification

How to use php+AJax+json to implement login verification

PHPz
PHPzOriginal
2023-03-17 20:35:531615browse

With the development of WEB2.0 and AJAX, more and more sites use AJAX technology to asynchronously load some pages. As a popular web development language, PHP can achieve some cool effects when combined with AJAX. This article will introduce how to use AJAX and JSON to implement basic login verification functions.

First we need to prepare the following files: index.html, login.php, user.json. Among them, index.html is the homepage of the website, login.php is used to process login requests, and user.json is used to store user information.

1. Design of index.html

In index.html we need to design a login form, which consists of two fields: username and password. To facilitate AJAX calls, you can add the id attribute to the form.




    
    登录
    

    
        
        
             
    
    

2. Writing of login.php

login.php is mainly used to process login requests. The login request needs to determine whether the username and password are correct. If they are correct, a string in JSON format will be returned to indicate successful login. Otherwise, a login failure message will be returned.

 1, 'msg' => '登录成功');
} else {
    $result = array('status' => 0, 'msg' => '用户名或密码错误');
}
echo json_encode($result);

3. Writing of user.json

user.json stores user name and password information. This file can be generated in various ways, such as manually written, exported from a database, etc.

{
    "username": "admin",
    "password": "123456"
}

4. Writing of login.js

login.js is mainly used to process the submission request of the login form and send the form data to login.php through AJAX. , the login result is returned to the page through the callback function.

$(function(){
    $('#login-btn').click(function(){
        $.ajax({
            type: 'POST',
            url: 'login.php',
            data: $('#login-form').serialize(),
            dataType: 'json',
            success: function (data) {
                if (data.status === 1) {
                    $('#msg').html(data.msg).css('color', 'green');
                } else {
                    $('#msg').html(data.msg).css('color', 'red');
                }
            }
        });
    });
});

In the above code, first we get the ID of the login button through the jQuery selector, and then call the AJAX method in the click event. In the AJAX method, we define the request type and address, as well as the data to be sent to login.php, and specify the data type as JSON.

In the callback function, we perform logical operations based on the returned data. If the login is successful, a success message is displayed, otherwise a failure message is displayed.

At this point, a basic login verification function has been implemented. Through the collaboration of AJAX and JSON, we can achieve a more efficient Web development model, making the user experience smoother and developers more efficient.

The above is the detailed content of How to use php+AJax+json to implement login verification. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn