开发者

Can not work correctly with posting form with jQuery

I am trying to get the return from the mail.php but it does not work. What am I missing here?

this is my form

<form name="myform" id="myform" action="" method="POST"> 
<input type="text" name="email" id="email" size="30" value=""/> 
<input type="submit" name="button" id="button" value="Αποστολή">
</form>

and this is the script

$(function() {
$("#myform").submit(function() {

$.post('mail.php', function(data) {
  $('#results').html(data); });
});
});

this script works correctly with jQuery Validate

$(document).ready(function(){
    $("#myform").validate({

        submitHandler: fun开发者_如何学Cction(form) {
            // do other stuff for a valid form
            $.post('mail.php', $("#myform").serialize(), function(data) {
                $('#results').hide().html(data).fadeIn('slow');
            });
        }
    });
});


$(function() {
   $("#myform").submit(function(event) {

        $.post('mail.php', function(data) {
            $('#results').html(data); });
        });

        event.preventDefault();
    });
})

Cancel the form submission because you are already doing it in ajax.

Update

This is how it should look like without validation:

$(function(){
    $("#myform").submit(function(event){
        event.preventDefault();

        // do other stuff for a valid form
        $.post('mail.php', $("#myform").serialize(), function(data) {
            $('#results').hide().html(data).fadeIn('slow');
        });
    });
});

The difference is you are sending the serialized data from (with $("#myform").serialize() ) the form. In the previous code you were not sending any variables. Serializing meangs it will take the values of the forms and concatenate them into a single string. Read more about this here: http://api.jquery.com/serialize/


Doesn't look like you are not sending the form data:

 $("#myform").submit(function(event) { 

     event.preventDefault(); 

     $.post("mail.php", $("#myform").serialize(), 
          function(data) {
            $('#results').html(data); 
     });


});

Preventing default behavior before ajax calls prevents the form from being submitting if the ajax has an error or for some reason causes the js to stop.


$(function() {
    $("#myform").submit(function(e) {

        e.preventDefault();

        var data = $(this).serialize();

        $.post('mail.php', data, function(response) {
            $('#results').html(response);
        });

    });
});

You had a syntax error plus you didn't prevent the default click event.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜