Ajax form submit using jQuery

The basic script code, through which we submit a form using an Ajax call without refreshing the page using jQuery is as follows:

<script type=”text/javascript”>
var baseurl = $(“#baseURL”).val();
var customURL = baseurl + “path/to/controller/to/execute/insert”;
$.ajax({
    type: ‘POST’,
    url: customURL,
    data: $(“#form_id”).serialize(),
    success: function(response) {
        // based on the response generally, we display messages.
        if (response) {
            $(“#location_to_display_success”).html(“Form Submission Successful.”);
        } else {
            $(“#location_to_display_errors”).html(“Form Submission Failed. Please try again.”);
        }
    }
});
</script>

By, using this basic JavaScript, we can submit a form using Ajax.

Leave a comment