2

I'm trying to submit a form through AJAX instead of using the normal POST submission. The HTML is just a standard form with method="post" and my jQuery code is as follows:

jQuery.noConflict();
jQuery(document).ready( function($) {
    var $form = $('#default_contact');

    $form.submit( function() {
        $.ajax({
            type: 'POST',
            url: $form.attr( 'action' ),
            data: $form.serialize(),
            success: function( response ) {
                console.log( response );
            }
        });

        return false;
    });
});

(Based on this answer)

I'm returning false from the submit function, but the form is still getting submitted and I can't figure out why. Not getting any Firebug errors.

Community
  • 1
  • 1
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290

4 Answers4

5

your code works fine if html is setup right. here's my test html (with php), so you can compare to your own.

<?php
if (!empty($_POST)) {
  die("<pre>" . print_r($_POST, true) . "</pre>");
}
?>
<html>
<head>
  <title>ajax submit test</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
    jQuery.noConflict();
    jQuery(document).ready(function ($) {
        var $form = $('#default_contact');

        $form.submit(function () {
            $.ajax({
                type: 'POST',
                url: $form.attr('action'),
                data: $form.serialize(),
                success: function (response) {
                    alert(response);
                }
            });

            return false;
        });
    });
</script>      
</head>
<body>
  <form id="default_contact" action="formsubmit.php" method="post">
    <input type="hidden" value="123" name="in1" />
    <input type="hidden" value="456" name="in2" />
    <input type="submit" value="send" />
  </form>
</body>
</html>
thecodedeveloper.com
  • 3,220
  • 5
  • 36
  • 67
parserr
  • 729
  • 1
  • 5
  • 15
  • Turned out to be something really stupid - the jQuery library code wasn't being included. It was in a PHP conditional that was false on the page I was testing with... D'oh! – DisgruntledGoat Apr 27 '10 at 17:01
2

If you have your html set up so that the form works all on its own, then it will act just as a normal form without the jquery. So take out the action attribute from the html, and change your jquery to:

url: your-submit-file.php,

If that doesn't work, try:

jQuery(document).ready( function($) {
    var $form = $('#default_contact');

    $form.submit( function(event) {
        $.ajax({
            type: 'POST',
            url: your-submit-file.php,
            data: $form.serialize(),
            success: function( response ) {
                console.log( response );
            }
    });
    event.preventDefault;
    return false;
});

Also, have you tried jQuery's $.post() ? It's much easier to use...

Villapalos
  • 677
  • 9
  • 15
Tim
  • 6,986
  • 8
  • 38
  • 57
0

i had a similar problem- my problem was that during a form post, i was using $("form").submit to do something before the page submitted. this something included a jquery post action. unfortunatley, the post action wouldnt complete before the form post completed, and thus caused problems later- my code looked like this:

        $("form").submit(function () {
                var result =
                    $.post('/Controller/Action',
                    { data: array.toString() }
            });

The solution was to

        event.preventDefault;

immediately before return true, which stops the form posting until the ajax call has completed.

Tom Beech
  • 2,289
  • 1
  • 21
  • 25
0

i tried many option didnt work for me i used post instead

$.post("myUrl", $( "#hiddenForm" ).serialize() ).done(function(data){
 myMap = new Map(Object.entries(data));        
 .....
 ......
});
JustTry
  • 167
  • 1
  • 10