jqueryjqueryform

jQuery Form does not submit


I'm not able to submit the form. Where I'm going wrong?

<div>
    <form id = "search_form" action = "/dashboard/" method = "post">
        <span class = "pull-right">
        Search : <input type = "text" placeholder = "Search" id = "search">
        </span>
    </form>
</div>

jQuery

$("#search_form").submit(function(event) {
    event.preventDefault();
    var formdata = $(this).serialize();
    alert(formdata);
    $.ajax({
        type: "POST",
        url: "gallery.php",
        data: formdata,
        success: function(){alert('success');}
    });
});

Upon submitting, the flow does not goes into the jQuery function. Why?


Solution

  • Try:

    $(function() {
        $("#search_form").submit(function(event) {
            event.preventDefault();
            var formdata = $(this).serialize();
            alert(formdata);
            $.ajax({
                type: "POST",
                url: "gallery.php",
                data: formdata,
                success: function(){alert('success');}
            });
        });
    });
    

    Everything works fine: http://jsfiddle.net/J6e4G/