Bootsrtap+ajax issue: submit does't work

Larry Edison Source

I have form:

<form class="dark-bg iq-pall-30" action="" id="mainform" method="post">
...
</form>

Here is my "submit-button":

<a href="#" class="button" onclick="$('#mainform').submit();">Go</a>

That's working well. But when I try to use ajax it doesn't.

  $('#mainform').submit(function(){
        $.ajax({
            type: "POST",
            url: "link..",
            data: $("#mainform").serialize(),
            success: function(json){
                if (json['error']) {
                    $("#content22").html(json['result']);
                }
                if (json['result'])
                {
                    $( "#mainsec" ).empty();       
                    $("#content22").html(json['result']);          
                }
            }
        });
        return false;
    });

As a result, I got an empty page. What am I doing wrong?

javascriptphpajaxtwitter-bootstrap

Answers

answered 3 months ago Saurabh Mistry #1

Use This Way :

<form class="dark-bg iq-pall-30" action="" id="mainform" method="post">


</form>

<a href="#" type="button" class="button" onclick="submitForm();">Go</a>

   <script>
    function submitForm(){    
        $.ajax({
            type: "POST",
            url: "link..",
            data: $("#mainform").serialize(),
            success: function(json){
                if (json['error']) {
                    $("#content22").html(json['result']);
                }
                if (json['result'])
                {
                    $( "#mainsec" ).empty();       
                    $("#content22").html(json['result']);          
                }
            }
        });
    }
    </script>

comments powered by Disqus