HTML5本地数据库来做注册登录功能

注册

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>

    <body>
        用户名:<input type="text" name="" id="" value="" /> 密码:
        <input type="password" name="" id="" value="" />
        <input type="button" name="" id="btn" value="注册" />
        <button type="button" id="clear">清除数据</button>
    </body>
    <script type="text/javascript">
        var aInputs = document.getElementsByTagName('input');
        var oClear = document.getElementById('clear');
        var db = openDatabase('zuchedenglu', '1.0', 'textdb', '1024*1024');
        db.transaction(function(contex) {
            contex.executeSql('create table if not exists userinf(id unique,name,password)');
        });
        var oBtn = document.getElementById('btn');
        var num = 0;
        oBtn.onclick = function() {
            if (aInputs[0].value && aInputs[1].value) {
                db.transaction(function(contex) {
                    contex.executeSql('select * from userinf', [], function(con, data) {
                        var leg = data.rows.length,
                            i;
                        for (var i = 0; i < leg; i++) {
                            if (aInputs[0].value == data.rows.item(i).name) {
                                alert('该用户名已注册!');
                                return;
                            }
                        }
                        num =leg +1;
                    });
                    contex.executeSql('insert into userinf(id,name,password) values("' + num + '","' + aInputs[0].value + '","' + aInputs[1].value + '")');
                    alert('注册成功');
                });
            } else {
                alert('请填写完整的账号密码!');
            }
        }
        oClear.onclick=function(){
            db.transaction(function(contex) {
                contex.executeSql('drop table userinf');
            });
        }
    </script>

</html>

登录

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>

    <body>
        用户名:<input type="text" name="" id="" value="" /> 密码:
        <input type="password" name="" id="" value="" />
        <input type="button" name="" id="btn" value="登陆" />
        <button type="button" id="clear">清除数据</button>
    </body>
    <script type="text/javascript">
        var aInputs = document.getElementsByTagName('input');
        var oClear = document.getElementById('clear');
        var db = openDatabase('zuchedenglu', '1.0', 'textdb', '1024*1024');
        db.transaction(function(contex) {
            //  tx.executeSql('drop table userinf');
            //contex.executeSql('create table if not exists userinf(id unique,name,password)');
            contex.executeSql('create table if not exists userinf(id integer primary key AutoIncrement,name,password)');
        });
        var oBtn = document.getElementById('btn');
        var blogin = true;
        oBtn.onclick = function() {
            if (aInputs[0].value && aInputs[1].value) {
                db.transaction(function(contex) {
                    contex.executeSql('select * from userinf', [], function(con, data) {
                        var leg = data.rows.length,
                            i;
                        for (var i = 0; i < leg; i++) {
                            if (aInputs[0].value == data.rows.item(i).name && aInputs[1].value == data.rows.item(i).password) {
                                blogin = false;
                                break;
                            }
                        }
                        if (blogin) {
                            alert('请输入正确的账号和密码!');
                        } else {
                            alert('登陆成功!');
                        }
                    });
                });
            } else {
                alert('请填写完整的账号密码!');
            }
        }
        oClear.onclick = function() {
            db.transaction(function(contex) {
                contex.executeSql('drop table userinf');
            });
        }
    </script>

</html>
  • 23
    点赞
  • 141
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
HTML本身不支持直接连接本地SQL Server数据库,因此你需要使用一种服务器端脚本语言(如PHP、ASP.NET或Java)来处理与数据库的交互。 以下是一个使用PHP连接本地SQL Server数据库的示例代码: ```php <?php $serverName = "localhost"; // 替换为你的SQL Server实例名或IP地址 $connectionInfo = array( "Database"=>"yourDatabaseName", "UID"=>"yourUserName", "PWD"=>"yourPassword"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo "连接成功!"; }else{ echo "连接失败!"; } ?> ``` 在这个示例中,你需要将 `$serverName` 替换为你的SQL Server实例名或IP地址,将 `yourDatabaseName` 替换为你的数据库名称,将 `yourUserName` 替换为你的SQL Server用户名,将 `yourPassword` 替换为你的SQL Server密码。 然后,你可以使用SQL查询语句来从数据库中检索数据,将结果显示在HTML页面上。你可以使用HTML和CSS来设计你的登录注册页面,使用JavaScript来实现与服务器端的交互,发送表单数据并获取响应结果。 需要注意的是,由于HTML本身是静态的,无法处理动态交互,因此你需要使用其他技术来实现登录注册的功能。常见的法是使用AJAX技术,通过JavaScript发送异步请求,获取服务器端返回的数据并进行处理。另一种方法是使用服务器端重定向,在登录注册成功后将用户重定向到其他页面,以避免在页面上显示敏感信息。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值