zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    二人pk答题小程序
    题解【CF911F】Tree Destruction
    题解【洛谷P5765】[CQOI2005]珠宝
    WC2021 游记
    生产环境财务BUG的排查与总结
    生产环境一个订单状态错误的排查与反思
    《HeadFirstServletAndJsp》笔记一
    Java泛型简介二
    Java泛型简介一
    在Windows环境下与Linux环境下快速计算文件Hash
  • 原文地址:https://www.cnblogs.com/meroselove/p/2212535.html
Copyright © 2011-2022 走看看