开发者

Send asapMail without reloading page using ajax

开发者 https://www.devze.com 2022-12-12 09:34 出处:网络
I have a form that posts data to an aspMail file for delivery (works fine in current format), but would like to have the form data sent without having to re-direct to another page. Looks like Ajax is

I have a form that posts data to an aspMail file for delivery (works fine in current format), but would like to have the form data sent without having to re-direct to another page. Looks like Ajax is the way to proceed but 开发者_StackOverflowI'm having a problem getting the setup to work.

Here's how I've changed the html:

Added:

$('#myForm').submit(function() {  
    $.ajax({  
        data: $(this).serialize(),  
        type: $(this).attr('post'),  
        url: $(this).attr('aspSend.asp'),  
        success: function(response) {  
            $('#created').html(response)  
        }  
    });  
});  

Changed the form tag from:

<form name="myForm" action"aspSend.asp">

to:

<form name="myForm">

but now the emails don't arrive!! Any suggestions?


First off, I'd suggest you use Firebug to see exactly what's happening when you're hitting submit (is it actually posting? What's in the POST? What's the response?).

Aside from that, a few thoughts:

  1. Make sure the form has an ID of myForm, as #myForm refers to the ID of an element, not the name of the element (so add id='myForm' to teh form tag)
  2. I'm not sure about your attr tags to get the post information (maybe they work, but I haven't seen them look like that). I would do your post like this:

     $('#myForm').submit(function() {
        dataString = $("#myForm").serialize();
        $.ajax({
        type: "POST",
        url: "aspSend.asp",
        data: dataString,
        dataType: "json",
        success: function(data) {
            if(data.status == "invalid"){
                $("#created").html("<div>Error</div>");
            } else {
                    $("#created").html("<div>Success</div>");
            }
        }
      });
    return false;            
    

    });


Are you sure the code snippet with form-html is correct? You jQuery looks for an element with id myForm. But your form doesn't seem to have and id attribute.

So either add the id to form or try replacing this line

$('#myForm').submit(function() {

with this one

$('form[name="myForm"]').submit(function() {

And add a return false; statement after the ajax call

$.ajax({...});
return false;
0

精彩评论

暂无评论...
验证码 换一张
取 消