ajax无刷新表单提交、验证码的使用

11 篇文章 0 订阅

无刷新网页表单使用

1、给表单添加验证

    //注册验证
    $("#myform").valid([
        { name: "reg_name", type: "text", isNull: "用户不能为空", onFocus: "请填写用户!" },
        { name: "reg_email", type: "mail", isNull: "邮箱不能为空", onFocus: "邮箱格式(*@*.*)", error: "邮箱格式不正确" },
        { name: "reg_password", type: "password", isNull: "密码不能为空", onFocus: "请填写6-18位密码", error: "请填写6-18位密码" },
        { name: "reg_confirm", type: "eq", error: "两次输入密码不一致", other: { to: "reg_password" } },
        { name: "reg_code", type: "ajax", error: "验证码不正确", other: { url: "/Handlers/RegHandler.ashx" } }

    ], true);

2写入ajaxForm()


//提交注册信息
    function ajaxForm() {
        $.ajax({
            type: "POST", //设置请求发送的方式
            timeout: 30000, //设置服务器请求超时时间
            url: "/Handlers/UpHandler.ashx", //提交的地址
            data: $("#myform").serialize(), //序列化表单元素值
            beforeSend: function () {//表单提交前执行的函数
                alert("text");
                $("#errordiv").text("服务器超时,请稍后再试![关闭]");
                $("#errordiv").hide();
                $("#zzc").show();
                popupDiv("popdiv");
            },
            error: function () {//提交发生错误的时候执行的函数
                $("#infodiv").hide();
                $("#errordiv").show();
            },
            success: function (data) {//提交成功的时候执行的函数
                if (data == "success") {
                    $("#infodiv").hide();
                    $("#regsuccess").show();
                    setInterval(MyTimer, 1000)
                } else {
                    $("#infodiv").hide();
                    $("#errordiv").text(data);
                    $("#errordiv").show();
                }
            }
        });
    }//)

3、后台接受采用一般处理程序

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //如果用户直接访问handler则拒绝
            if (context.Request.UrlReferrer == null)
            {
                context.Response.Write("请求错误!拒绝访问!");
                return;
            }
            //获取请求来路的完整的url
            string url = context.Request.UrlReferrer.AbsoluteUri;
            if (!url.Contains("/CompanyDishes/DishesBooks"))
            {
                context.Response.Write("请求错误!拒绝访问");
                return;
            }
            DishBook objBook = new Book()
            {
                CustomerName = context.Request.Params["CustomerName"],
                HotelName = context.Request.Params["HotelName"],
                ConsumeTime = Convert.ToDateTime(context.Request.Params["ConsumeTime"]),
                CustomerPhone = context.Request.Params["CustomerPhone"],
                CustomerEmail = context.Request.Params["CustomerEmail"],
                ConsumePersons = Convert.ToInt32(context.Request.Params["ConsumePersons"]),
                RoomType = context.Request.Params["selectRoomType"],
                Comments = context.Request.Params["Comments"] == "" ? "无" : context.Request.Params["Comments"],
            };
            try
            {
                //提交数据
                int count = new BookManager().AddBook(objBook);
                if (count > 0)
                    context.Response.Write("success");
                else
                {
                    context.Response.Write("error");
                }
            }
            catch (Exception ex)
            {
                context.Response.Write("提交失败!" + ex.Message);
                throw;
            }
        }


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值