美文网首页
48.项目 1-博客前端:封装库--Ajax 登录

48.项目 1-博客前端:封装库--Ajax 登录

作者: 好像在哪见过你丶 | 来源:发表于2019-05-20 09:35 被阅读0次

    学习要点:

    1.问题所在
    2.设置代码

    表单的目的就是实现用户的填写和提交,传统的提交需要提交到一个指定页面,需要卸
    载当前页面,然后加载到另外一个服务器端页面进行处理,最后再跳转到指定的页面。这种
    用户体验不是特别的很好。而 Ajax 则解决这些问题。

    一.问题所在

    QQ截图20190520093351.png

    二.设置代码

    HTML 代码

    <div class="info"></div>
    <div class="info"></div>
    

    CSS 代码

    #header .login, #header .reg, #header .info{
    float:right;
    width:35px;
    height:30px;
    line-height:30px;
    cursor:pointer;
    }
    #header .info {
    width:80px;
    display:none;
    }
    #login div.info {
    padding:15px 0 5px 0;
    color:maroon;
    text-align:center;
    }
    

    JS 代码

    $('form').eq(1).form('sub').click(function () {
    if (/[\w]{2,20}/.test(trim($('form').eq(1).form('user').value()))
    && $('form').eq(1).form('pass').value().length >= 6) {
    var _this = this;
    _this.disabled = true;
    $(_this).css('backgroundPosition', 'right');
    $('#loading').css('display', 'block').center(200, 40);
    $('#loading p').html('正在尝试登录...');
    ajax({
    method : 'post',
    url : 'is_login.php',
    data : $('form').eq(1).serialize(),
    success : function (text) {
    $('#loading').css('display', 'none');
    _this.disabled = false;
    $(_this).css('backgroundPosition', 'left');
    if (text == 1) {//失败
    $('#login .info').html('登录失败,用户名或密码不正确!');
    } else {//成功
    setCookie('user', trim($('form').eq(1).form('user').value()));
    $('#login .info').html('');
    $('#success').css('display', 'block').center(200, 40);
    $('#success p').html('登录成功...');
    setTimeout(function () {
    $('#success').css('display', 'none');
    login.css('display', 'none');
    $('form').eq(1).first().reset();
    screen.animate({
    attr : 'o',
    target : 0,
    t : 30,
    step : 10,
    fn : function () {
    screen.unlock();
    }
    });
    $('#header .reg').css('display', 'none');
    $('#header .login').css('display', 'none');
    $('#header .info').css('display', 'block').html(getCookie('user') +
    ',您好!');
    }, 1500);
    }
    },
    async : true
    });
    } else {
    $('#login .info').html('登录失败,用户名或密码不合法!');
    }
    });
    

    PS:由于登录也有一个 form,会导致之前的注册的 form 失效,必须精确的选择才行。
    cookie 操作,直接复制基础课程封装的代码即可。

    //判断用户名和密码
    <?php
    require 'config.php';
    $_pass = sha1($_POST['pass']);
    $query = mysql_query("SELECT user FROM blog_user WHERE
    user='{$_POST['user']}' AND pass='{$_pass}'") or die('SQL 错误!');
    if (!mysql_fetch_array($query, MYSQL_ASSOC)) {
    echo 1;
    }
    mysql_close();
    ?>
    

    感谢收看本次教程!

    相关文章

      网友评论

          本文标题:48.项目 1-博客前端:封装库--Ajax 登录

          本文链接:https://www.haomeiwen.com/subject/njjhzqtx.html