Stopping the form from refreshing, jquery, ajax and Laravel

Posted 2 years ago by agahi

Hi again I have a form and some Jquery that submit it through ajax to another page. The problem is upon submit the page gets refreshed. I tried lots of things but either the form submission doesn't work or page refreshes. This is the form tag:

{!! Form::open(['url' => 'comments','id' => 'commentForm']) !!}

And the script:

$(document).ready(function() { 
  $('#commentForm').validate({
    rules: {
        body: {
            required:true,
            minlength: 6
        },
        messages: {
            "body": {
                required: "a message",
                minlength: "a message"
            }
        }
    },
    submitHandler: function() {
        $('#formDiv').html("<span style='color:red;'>A message</span>");        
        $('#commentForm').ajaxSubmit();             
    }       
  });
}); 
```

Could you help me altering to code to solve the problem. And would you say this is the best practice to validate and post through Laravel and Ajax?

Please sign in or create an account to participate in this conversation.