phpjqueryajaxlaravelforms

Post a form via jquery from a same page where url is slightly changed but working on one url and refreshes the page on other url


I'm facing a weird problem where I have to reach a form page via two methods: create and edit. When posting from the create URL, the same form works perfectly, but whenever I post via edit or re-create URL, the page refreshes itself and tries to post on the URL itself.

Urls are: http://localhost:8000/admin/orders/create http://localhost:8000/admin/orders/create/1234

and the code is:

$('#quote_form').validate({
    rules: {
        item: {
            required: true,
        },
        quantity: {
            required: true,
        }
    },
    focusInvalid: true,
    invalidHandler: function(form, validator) {
        $('#' + validator.errorList[0].element.id).focus();
    },
    submitHandler: function() {
        $('#newQuote').empty();
        let dataArray = $('#quote_form').serializeArray();
        let item = $('#item').val();
        let customerValue = '';
        if ($('#customer').val().length !== 0 && $('#customer').val() !== undefined) {
            customerValue = $('#customer').val();
        } else if ($('#customer_id').val().length !== 0 && $('#customer_id').val() !== undefined) {
            customerValue = $('#customer_id').val();
        }
        dataArray.push({
            name: 'customer',
            value: customerValue
        });
        $.ajax({
            url: ADMIN_AJAX_URL + "orders/create-order-session",
            method: 'post',
            data: dataArray,
            success: function(response) {
                let result = JSON.parse(response);
                if (result.html != '') {
                    clearForm($("#quote_form"));
                    $('#newQuote').append(result.html);
                } else {
                    $('.error_text').html(result.message);
                    $('.error_row').show();
                }
            }
        });
    }
});

I've tried to post via the submit button and form validation etc, but not bored fruit.


Solution

  • To prevent the default form submission behavior, pass event.preventDefault() in your submitHandler.The submitHandler function receives a form parameter, so you should pass that into $(form).serializeArray() instead of using $('#quote_form').serializeArray().

    $('#quote_form').validate({
        rules: {
            item: {
                required: true,
            },
            quantity: {
                required: true,
            }
        },
        focusInvalid: true,
        invalidHandler: function(form, validator) {
            $('#' + validator.errorList[0].element.id).focus();
        },
        submitHandler: function(form, event) {
            event.preventDefault();
            $('#newQuote').empty();
            let dataArray = $(form).serializeArray();
            let item = $('#item').val();
            let customerValue = '';
    
            if ($('#customer').val().length !== 0 && $('#customer').val() !== undefined) {
                customerValue = $('#customer').val();
            } else if ($('#customer_id').val().length !== 0 && $('#customer_id').val() !== undefined) {
                customerValue = $('#customer_id').val();
            }
    
            dataArray.push({
                name: 'customer',
                value: customerValue
            });
    
            $.ajax({
                url: ADMIN_AJAX_URL + "orders/create-order-session",
                method: 'post',
                data: dataArray,
                success: function(response) {
                    let result = JSON.parse(response);
                    if (result.html != '') {
                        clearForm($("#quote_form"));
                        $('#newQuote').append(result.html);
                    } else {
                        $('.error_text').html(result.message);
                        $('.error_row').show();
                    }
                }
            });
        }
    });