Form submit (email) with ASP.net and jQuery not working
I am trying to figure this out, but I have not had much luck.
Basically, I have a wizard style HTML form that is built using the jQuery form wizard plugin, which includes jQuery.Form, jQuery.Validation, and jQuery.History. I'm trying to use ASP.net web forms to send email, but I can't seem to get it to work at all.
here is an example of my markup and code:
<form id="form1" runat="server">
<div class="step" id="step01">
<input type="text" id="text1" name="text1" runat="server" /><br />
<input type="text" id="text2" name="text2" runat="server" />
</div>
<div class="step" id="step02">
<input type="text" id="text3" name="text3" runat="server" /><br />
<input type="text" id="text4" name="text4" runat="server" />
</div>
<input type="reset" id="reset" value="Reset" />
<asp:Button runat="server" id="submit" Text="Submit" OnClick="button_Click" />
</form>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
public partial class email_test02 : System.Web.UI.Page
{
protected void button_Click(object sender, EventArgs e)
{
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("test@test.com");
mail.To.Add("test2@test.com");
//set the content
mail.Subject = "Test";
mail.Body =
text1.Value + text2.Value + text3.Value + text4.Value;
//send the message
SmtpClient smtp = new SmtpClient("Localhost");
smtp.Send(mail);
}
}
I'm not sure what I need to do from here. I'm still pretty new to jQuery and very new to ASP.net, so I'm sure I was just missing something obvious, but I had no luck with anything I tried. So if someone can point me in the right direction, it would be greatly appreciated.
a source to share
If you are using jquery form to submit, remove the onclick and give the form id. The id goes into your javascript code on the page to collect your message.
<script type="text/javascript">
$(document).ready(function() {
$('#form1').ajaxForm(function() {
location.href=("submitted.aspx");
});
});
</script>
in page load just add
if(isPostback){
string txt1 = Request["text1"];
// you will want to clean this string, (encode for storage)
// and maybe also check for isnull or empty first i.e
var username = !string.IsNullorEmpty(Request["text1"]) ? Request["text1"] : null;
}
a source to share