Blogger Information
Blog 28
fans 2
comment 0
visits 23343
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
7.16表单事件与AJAX表单验证
背着吉他的女侠
Original
585 people have browsed it

1. Ajax - GET 请求获取数据

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ajax-GET方法处理表单</title>
</head>
<body>
<label for="user-id">输入用户ID:</label>
<input type="text" id="user-id" name="user_id" autofocus>
<p id="tips"></p>

<script>
    var input = document.getElementById('user-id');
    var tips = document.getElementById('tips');

    var request = new XMLHttpRequest();

    input.addEventListener('keypress', getUserInfo, false);

    function getUserInfo(ev) {
        if (ev.key === 'Enter') {
            switch (true) {

                case input.value.length === 0:
                    tips.innerHTML = '<span style="color: red">ID不能为空</span>';
                    return false;

                case isNaN(input.value):
                    tips.innerHTML = '<span style="color: red">ID必须是整数</span>';
                    return false;

                default:
                    request.addEventListener('readystatechange', successCallback, false);


                    request.open('GET', 'php/user_info.php?user_id='+data, true);

                    request.send(null);
            }
        }
    }

    function successCallback() {
        if (request.readyState === 4) {
            tips.innerHTML = request.responseText;

        }
    }

    input.addEventListener('input', function () {
        tips.innerHTML = null;
    }, false);


</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

2. Ajax - POST 请求数据

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ajax表单验证信息并处理</title>
</head>
<body>
<h2>用户登录</h2>
<form action="" name="login">
    <p>
        <label for="username">用户:</label>
        <input type="text" name="username" id="username" placeholder="UserName">
    </p>
    <p>
        <label for="email">邮箱:</label>
        <input type="text" name="email" id="email" placeholder="example@email.com">
    </p>
    <p>
        <label for="password">密码:</label>
        <input type="password" name="password" id="password" placeholder="******" >
    </p>
    <p>
        <label for="remember">记住我:</label>
        <select name="remember" id="remember">
            <option value="1" selected>一天</option>
            <option value="7">一星期</option>
            <option value="30">一个月</option>
        </select>
    </p>
    <p>
        <button type="button" name="submit">提交</button>
    </p>
</form>
<script>
 var login = document.forms.namedItem('login');
 var btn=login.submit;
 //创建Ajax请求对象
    var request = new XMLHttpRequest();

    // 表单的一些验证

    login.username.addEventListener('blur',isEmpty,false);
    login.email.addEventListener('blur',isEmpty,false);
    login.password.addEventListener('blur',isEmpty,false);


    function isEmpty(ev) {

        if(ev.target.value.length===0){

            if{ev.target.nextElementSibling===null}{

                var tips = document.createElement('span');

                tips.style.color = 'red';

                switch (ev.target.name) {
                    case 'username':
                        tips.innerText = '用户名不能为空';
                        break;
                    case 'email':
                        tips.innerText = '邮箱不能为空';
                        break;
                    case 'password':
                        tips.innerText = '密码不能为空';
                        break;
                    default:
                        tips.innerText = '未定义错误';
                }

                ev.target.parentNode.appendChild(tips);

                ev.target.focus();


            }

        }

        login.username.addEventListener('input', clearTips, false);
        login.email.addEventListener('input', clearTips, false);
        login.password.addEventListener('input', clearTips, false);

        function clearTips(ev) {

            var tips = ev.target.nextElementSibling;
            if (tips !== null) {

                tips.parentNode.removeChild(tips);
            }
        }

        btn.addEventListener('click', check, false);

        function check(ev) {
            var username = login.username.value;
            var email = login.email.value;
            var password = login.password.value;
            var remember = login.remember.value;

            if (username.length === 0 || email.length === 0 || password.length === 0) {
                ev.target.removeEventListener('click', check, false);
                var blurEvent = new Event('blur');
                login.username.dispatchEvent(blurEvent);
                return false;
            }
            var data = 'username='+username+'&email='+email+'&password='+password+'&remember='+remember;

            request.addEventListener('readystatechange', successCallback, false);

            request.open('POST', 'php/check.php', true);

            request.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=utf-8');

            request.send(data);
        }

        function successCallback(ev) {
            if (request.readyState === 4) {
                console.log(request.responseText);
            }
        }






</script>
</body>

运行实例 »

点击 "运行实例" 按钮查看在线实例

Correction status:qualified

Teacher's comments:课堂案例,还是要多看看 ,php部分先忽略
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post