I've got the flow all worked out thanks to balexandre and rtiq. My .ashx file is being called so I know a portion of the code is working and it is alerting me to an error. When I trace the .NET, the variables pulled in via context.Request["email"] and context.Request["optin"] are NULL.
I know there's something wrong but I can't see it. I've re-edited this post to have the latest code.
jQuery in HEAD
<script type="text/javascript">
$(document).ready(function () {
$(".submitConnectButton").click(function (evt) {
evt.preventDefault();
alert("hello click");
alert($(".emailConnectTextBox").val());
$.ajax({
type: "POST",
url: "/asynchronous/insertEmail.ashx",
data: "{email: '" + $(".emailConnectTextBox").val() + "',optin: '" + $(".connectCheckbox").val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) { alert(msg.d); },
error: function (msg) { alert('Error:' + msg); }
});
});
});
</script>
HTML
<div class="emailConnect">
<asp:TextBox runat="server" ID="TextBox1" CssClass="emailConnectTextBox" BorderStyle="Solid"></asp:TextBox>
<asp:ImageButton id="connectButton" CssClass="submitConnectButton" runat="server" ImageUrl="~/Images/submit_btn.png" /><br />
<asp:CheckBox Checked="true" id="checkbox1" runat="server" CssClass="connectCheckbox" />
</div>
CodeBehind in a .ashx
public class insertEmail : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string strConnection = System.Configuration.ConfigurationManager.AppSettings["SQLConnectString"].ToString();
string email = context.Request["email"],
optin = context.Request["optin"];
string strSQL = "INSERT INTO Emails (emailAddress,optIn) VALUES('" + email.ToString() + "','" + optin.ToString() + "')";
SqlConnection Conn = new SqlConnection(strConnection);
SqlCommand Command = new SqlCommand(strSQL, Conn);
Conn.Open();
Command.ExecuteNonQuery();
Conn.Close();
context.Response.ContentType = "text/plain";
context.Response.Write("email inserted");
}
public bool IsReusable
{
get
{
return false;
}
}
}
The form and elements are acting properly. We are just getting this NULL values and not being able to insert. The ajax is calling the .ashx file properly and the file is compiling, the requested variables are null.. The previous help was awesome, if anyone could help me get this last kink out, you would get a gold star for the day! :)
After some searching offline in books, this finally worked for me in concjunction with balexandres .aspx method:
SOLUTION
$.post("/asynchronous/addEmail.aspx", {email: $(".emailConnectTextBox").val(),optin: $(".connectCheckbox").is(':checked')}, function(data) { alert('Successful Submission');});