• Ajax 判断数据库表中该用户是否存在


    index.html

    <script type="text/javascript">


            var xmlHttp;


            //创建XMLHttpRequest对象
            //目的:兼容浏览器
            function createXMLHttpRequest() {
                if (window.ActiveXObject) {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
                }
                else if (window.XMLHttpRequest) {
                    xmlHttp = new XMLHttpRequest();
                }
            }


            function StringBuilder() {

                var userName = document.getElementById("User").value;

                return encodeURI(encodeURI(userName)); //两次编码解决中文乱码问题
            }

       //当状态改变的时候

            function StateChange() {
                if (xmlHttp.readyState == 4 && xmlHttp.Status == 200) {
                    var responseDiv = document.getElementById("back");
                    responseDiv.innerHTML = decodeURI(xmlHttp.responseText); //解码
                }
            }

            //发送

            function SendToPage() {

                createXMLHttpRequest();

                var queryString = "Page_1.aspx?U=";
                queryString += StringBuilder();

                xmlHttp.open("GET", queryString);

                xmlHttp.onreadystatechange = StateChange;

                xmlHttp.send(null);
            }

    <body>
        用户名:<input type="text" name="UserName" id="User" />
        <input type="button" name="btn" value="Show" onclick="SendToPage();" /><br />
        <div id="back">
        </div>
    </body>

    IsExists.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
            {
                if (Request.QueryString["U"] != null)
                {
                    Session["User"] = Request.QueryString["U"].ToString();
                    Response.Write(Check());
                    Response.End();
                }
            }

            protected string Check()
            {
                string sql = string.Empty;
                string res = string.Empty;

                if (Session["User"] != null)
                {
                    sql = "select count(*) from T_Person where C_Name='" + Session["User"].ToString() + "'";
                }
                else
                {
                    res = "error";
                    return res;
                }

                if ((int)new SQLHelper().ExecuteScalar(sql) == 0)
                {
                    res = "不存在该用户名!";
                }
                else
                {
                    res = "已存在该用户名!";
                }
                return res;
            }


                    
        </script>

  • 相关阅读:
    HTML导航条的制作
    图片样式加hover特效
    用表格制作商品购买页面--(table)
    CSS-盒子模型
    一些常见css样式加选择器
    css的一些样式
    HTML基本代码
    element-ui的tab切换同步步骤条 字符串转数字 数字转字符串
    vuex相关知识笔记
    js: 数组方法(中级)
  • 原文地址:https://www.cnblogs.com/meroselove/p/2212535.html
Copyright © 2020-2023  润新知