$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Post a form via jquery from a same page where url is slightly changed but working on one url and refreshes the page on oth|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin14PV0评论

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.

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.

Share Improve this question asked Nov 16, 2024 at 2:14 Abid RizviAbid Rizvi 131 silver badge3 bronze badges 3
  • ... but not bored fruit ... what does this mean? – Paul T. Commented Nov 16, 2024 at 4:36
  • @PaulT. I'd assume the OP meant "but it did not bear fruit". The idea of a fruit being bored and needing entertainment did make me smile though – ADyson Commented Nov 16, 2024 at 8:35
  • I think its "Bore Fruit" but people not give you margin of a single letter. Anyways I'm glad that you smiled on something. By the way I'm a web developer not an article writer. – Abid Rizvi Commented Nov 18, 2024 at 14:17
Add a comment  | 

1 Answer 1

Reset to default 1

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();
                }
            }
        });
    }
});
Post a comment

comment list (0)

  1. No comments so far