一、checkuser .ashx文件:
<%@ WebHandler Language="C#" Class="checkuser" %>
using System;
using System.Web;
using XXX.XXX.Biz;
using XXX.XXX.DAC;
public class checkuser : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string username = HttpUtility.UrlDecode(context.Request.QueryString["username"] == null ? "" : context.Request.QueryString["username"].ToString());
string telphone = HttpUtility.UrlDecode(context.Request.QueryString["telphone"] == null ? "" : context.Request.QueryString["telphone"].ToString());
string email = HttpUtility.UrlDecode(context.Request.QueryString["email"] == null ? "" : context.Request.QueryString["email"].ToString());
string content = HttpUtility.UrlDecode(context.Request.QueryString["content"] == null ? "" : context.Request.QueryString["content"].ToString());
string strCode = HttpUtility.UrlDecode(context.Request.QueryString["checkcode"] == null ? "" : context.Request.QueryString["checkcode"].ToString());
string ccc = context.Session["Code"].ToString();
if (strCode == ccc)
{
//username = "李三";
//telphone = "36985632";
//email = "ccc@126.com";
//content = "ABC";
Tb_OnlineMessageSys message_Sys = new Tb_OnlineMessageSys();
Tb_OnlineMessageData message_Data = new Tb_OnlineMessageData();
message_Data.Publishers = username;
message_Data.Telephone = telphone;
message_Data.Email = email;
message_Data.Questions = content;
DateTime dtime = DateTime.Now;
message_Data.CreateDate = dtime.ToString();
message_Data.UpdateDate = dtime.ToString();
message_Sys.Tb_OnlineMessageData = message_Data;
if (!message_Sys.InsertData())
{
context.Response.Write("留言失败,请重试!");
}
else
{
context.Response.Write("留言成功!您的问题及建议我们会在24小时内给您回复,谢谢合作!");
}
}
else
{
context.Response.Write("请输入正确的验证码!");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
二、JS文件 messages.js
function Submit_Message() {
username = document.getElementById("l_name").value;
telphone = document.getElementById("l_qq").value;
email = document.getElementById("l_email").value;
content = document.getElementById("l_content").value;
checkcode = document.getElementById("l_checkcode").value;
$.ajax({
type: "GET",
url: "iframe/checkuser.ashx",
data: 'username=' + encodeURI(username) + '&telphone=' + telphone + '&email=' + email + '&content=' + encodeURI(content) + '&checkcode=' + encodeURI(checkcode) + '&ranid=' + Math.random(),
success: function(msg) {
alert(msg);
},
error: function() {
alert('error');
}
});
}
三、引用Jquery文件
四、验证码 ASHX文件 verify_code.ashx
<%@ WebHandler Language="C#" Class="verify_code" %>
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.Configuration;
public class verify_code : IHttpHandler,IRequiresSessionState //为要使用session实现的接口
{
public void ProcessRequest (HttpContext context)
{
string checkCode = GetValidation(5); // 产生5位随机验证码字符
context.Session["Code"] = checkCode; //将字符串保存到Session中,以便需要时进行验证
System.Drawing.Bitmap image = new System.Drawing.Bitmap(70, 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
// 画图片的背景噪音线
int i;
for (i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
context.Response.ClearContent();
context.Response.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
/// <summary>
/// 随即获取验证码
/// </summary>
/// <param name="num">位数</param>
/// <returns>返回验证码</returns>
public string GetValidation(int num)
{
string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //"或者写汉字也行"
string validatecode = "";
Random rd = new Random();
for (int i = 0; i < num; i++)
{
validatecode += str.Substring(rd.Next(0, str.Length), 1);
}
return validatecode;
}
public bool IsReusable {
get {
return false;
}
}
}
四、Html文件
<script src="Js/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="tempjs/messages.js" type="text/javascript"></script>
<div class="block_d">
<table border="0" align="center" style="margin-bottom: 20px;" cellpadding="0" cellspacing="1"class="form_a">
<tr>
<td align="right">您的称呼:</td>
<td colspan="2"><input name="l_name" type="text" id="l_name" size="35" class="text" /><span class="right1">*</span></td>
</tr>
<tr>
<td align="right" > 电话:</td>
<td colspan="2"><input name="l_qq" type="text" class="text" id="l_qq" size="35" maxlength="12" /><span class="right1">*</span></td>
</tr>
<tr>
<td align="right">您的邮箱:</td>
<td colspan="2"><input name="l_email" type="text" id="l_email" size="35" class="text" /><span class="right1">*</span></td>
</tr>
<tr>
<td align="right">留言内容:</td>
<td colspan="2">
<textarea name="l_content" cols="50" rows="5" id="l_content" class="areatext" οnkeyup="SubStrConten()"></textarea>
<span class="right1">*</span>
<label id="contentMsg" style="color: Red">
限制200个字符
</label>
</td>
</tr>
<tr>
<td height="25" align="right" bgcolor="#ffffff">
验证码:
</td>
<td bgcolor="#ffffff">
<img src="iframe/verify_code.ashx" id="vimg" alt="看不清?点击更换" οnclick="changeCode()"/>
</td>
<td bgcolor="#ffffff">
<input name="l_checkcode" id="l_checkcode" size="12" maxlength="5" class="text" οnfοcus="this.select(); "/><label id="Label1" style="color: Red"></label>
</td>
</tr>
<tr>
<td height="25" colspan="3" align="center" bgcolor="#ffffff">
<input type="submit" name="submit" value=" 提 交 " οnclick="return Submit_Message()" class="text"/>
<input type="reset" name="submit2" value=" 重 置 " class="text"/>
</td>
</tr>
</table>
</div>