views:

884

answers:

4

I have used a jquery UI tabs plugin in my page and in one of the tab i have put a form. SO for example , the tab calls a page form.php which has the form tag of html. Now form.php uses ajax and jquery to submit the form. Does this sound feasible.

    $(document).ready(function(){

    $("form#formdata").submit(function() {

            var str = $("#formdata").serialize();
            $.ajax({
            type: "POST",
            url: "submit.php",
            data: $("#formdata").serialize(),
            success: function(msg){
                     alert(msg);
                    }
    });

return false; });

});

SO the above code comes in my form.php

The form gets submitted but , not via ajax, and am directed to submit.php I want to reflect the result in my UI tabs itself.

Please suggest..thanks

EDIT : i put preventdefault() and return false , but it did not help..

+1  A: 

put return false in the form submit function after the ajax call.

czarchaic
i have tried putting return false but it did not help..thanks a lot for a quick reply..appreciate..
noobcode
+1  A: 

event.preventDefault() is the preferred way to handle this

$(document).ready(function() {
  $('form#formdata').submit(function(event) {
    event.preventDefault();
    var str = $('#formdata').serialize();
    $.ajax({
      type: 'POST',
      url:  'submit.php',
      data: $('#editmachine').serialize(),
      success: function(msg) { alert(msg); }
    });
  });
});
Cheekysoft
thanks will try this and see if it works
noobcode
HEY CHEEKYSOGT, i tried what you suggested but it did not work..I put a alert immediately after $(document).ready(function()) but it did not execute..How do i know whether my jquery is even being executed or not..thanks
noobcode
@cheekysoft, sorry for the typo mistake..
noobcode
note that you need to pass an argument into your anonymous function that you declare as the click handler. See I added the "event" argument.
Cheekysoft
if you have any error in your javascript code, all further javascript will not execute. Ensure you have the javascript console open to see any errors (Tools|Error Console in Firefox), or use a tool like Firebug to closely monitor what is happening.
Cheekysoft
A: 

Hi, you have to prevent that behavior with return false into submit event:

$(document).ready(function(){

    $("form#formdata").submit(function() {

            var str = $("#formdata").serialize();
            $.ajax({
            type: "POST",
            url: "submit.php",
            data: $("#editmachine").serialize(),
            success: function(msg){
                     alert(msg);
                    }
    });
return false;
    });

});
returnvoid
A: 

Finally i got it working.

This is what i did.

in my UI tabs code

<div id="tabs">
            <ul>
                <li><a href="link.html">Link1</a></li>
                <li><a href="form.html">Data</a></li>
            </ul>
</div>

The form.html contained the form details and from there i was using ajax.

SO to correct it i used the form details on the same page under a div like this

<div id="tabs">
                <ul>
                    <li><a href="link.html">Link1</a></li>
                    <li><a href="#form">Data</a></li>
                </ul>

<div id = "form">
//form details here
</div>

    </div>

And used the ajax jquery code on this page.

Thanks to all who replied to my question and special thanks to @cheekysoft

Hope this comes handy and useful..

noobcode