0

I'm using this form for ajax submission:

<form class="status-form" role="form" action="" enctype="multipart/form-data" id="statusupdateform" method="post">

And this javascript to do the ajax functions:

<script type="text/javascript">
    var frm = $('#statusupdateform');
    frm.submit(function (ev) {
        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                alert('ok');
            }
        });
        ev.preventDefault();
    });
</script>

But actually it refreshes the whole page.

How can I do this?

Nirmalya Ghosh
  • 2,298
  • 3
  • 18
  • 33
  • replace ev.preventDefault(); with return false; – hex494D49 Aug 15 '14 at 03:34
  • Does nothing different. Refreshes the whole page. – Nirmalya Ghosh Aug 15 '14 at 03:39
  • Where is your script code located? If it is in the `head` of the document or somewhere else before the `form` element, then you can not select the form elements this way, because it does not exist at that point – no submit handler bound, form is send the “normal” way. In that case, place your script code further down, or use the `ready` event. – CBroe Aug 15 '14 at 03:41
  • Impossible. This snippet `$("#form_id").submit(function() { // your code return false; });` won't let the form to get submitted – hex494D49 Aug 15 '14 at 03:42
  • Are any errors occurring? Is `$('#statusupdateform')` finding the `
    ` (`frm.length > 0`)?
    – Jonathan Lonowski Aug 15 '14 at 03:43
  • – Nirmalya Ghosh Aug 15 '14 at 03:45
  • The above code only shows a javascript alert 'okay' but it does get the queries to get executed which is at the top of the page! – Nirmalya Ghosh Aug 15 '14 at 03:46
  • Since the form uses `multipart`, note that jQuery's `.serialize()` won't include `type="file"` data. Sending files via Ajax generally means [using `FormData`](http://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax) and [supporting browsers](http://caniuse.com/#feat=xhr2). – Jonathan Lonowski Aug 15 '14 at 03:51
  • @JonathanLonowski Please check this [jsFiddle](http://jsbin.com/ziwomafuzide/1/edit?html,js,output) – hex494D49 Aug 15 '14 at 03:53
  • Is the form not getting submitted as the database queries are on the same page? – Nirmalya Ghosh Aug 15 '14 at 04:02

0 Answers0