无刷新网页表单使用
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;
- }
- }