简体   繁体   中英

Form submit (email) with ASP.net and jQuery not working

I'm trying to figure this out, but I'm not having much luck.

Basically, I have a wizard style HTML form that's built using the jQuery form wizard plugin, which includes the jQuery.Form, jQuery.Validation and jQuery.History plugins. I'm trying to use ASP.net Web Forms to submit an email, but I can't seem to make it work at all.

here's 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'm just missing something obvious, but I've had no luck with anything I've tried. So if someone could point me in the right direction here it would be much appreciated.

If you are using jquery form for the submittion remove the onclick and give the form an id. The ID goes into your javascript code on the page to collect your post.

<script type="text/javascript"> 
     $(document).ready(function() { 
        $('#form1').ajaxForm(function() { 
            location.href=("submitted.aspx");
        }); 
    }); 
</script> 

in your page load simply 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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM