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

I'm facing a weird problem where I have to reach a form page via two methods: create and edit. Whe

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745668423a4639257.html

相关推荐

  • Windows下配置Golang开发环境,并安装配置GoLand IDE

    作者:非妃是公主 专栏:《Golang》 博客地址:https:blog.csdnmyf_666 个性签:顺境不惰,逆境不馁

    1小时前
    00
  • 开发体育直播系统后台权限设计实践分享|ThinkPHP 技术栈落地案例

    今天我们分享的是一套由 东莞梦幻网络科技 自研的体育直播源码,在 ThinkPHP + MySQL 技术栈的加持下,后台权限系统如何从0到1落地,并支撑整个平台稳定运行。一、整体架构设计代码语言:html复制用户端(APPH5P

    1小时前
    20
  • 如何增加 Elasticsearch 中的主分片数量

    要增加现有索引的主分片数量,直接修改是不可能的。因此,如果你想增加主分片的数量,必须重新创建索引。通常有两种方法:_reindex API 和 _split API。在这两种方法中,_split API 通常比 _reindex API 更

    1小时前
    20
  • 如何使用python查询Prometheus监控数据

    一.环境准备软件库包版本python3.8prometheus-api-client0.5.7二.安装步骤代码语言:python代码运行次数:0运行复制pip3 install prometheus-api-client默认安装最新版本的p

    1小时前
    20
  • 怎么用html写出哆啦A梦?

    用HTML和CSS来画哆啦A梦(Doraemon)是一项有趣且具有挑战性的任务。虽然HTML和CSS主要用于网页布局和样式,但通过巧妙的组合和定位,可以创建出相对简单的图形和图案。下面是一个简单的示例,展示了如何用HTML和CSS绘制哆啦A

    1小时前
    00
  • 谷歌云第三方SSH工具登录与一键重装系统

    一,设置 root 密码 先选择从浏览器打开 ssh 连接服务器切换到 root 账号,输入代码:sudo -i设置 root 密码,输入代码&

    1小时前
    00
  • 用安信可Ai

    以下作品由安信可社区用户业余菜狗制作前言自从接触智能家居之后,笔者就变得很依赖智能家居(绝对不是懒!)比如卧室灯,就在进门的地方,进门开灯很方便,但是晚上睡觉关灯就很不方便。之前是买了一款Wi-Fi灯,是用手机APP操作,刚开始用的时候感觉

    1小时前
    00
  • Go项目实战

    上节课我给大家介绍了怎么给Go项目做单元测试的规划,当然这里仅限于跟咱们课程里的实战项目一样分层架构设计做的还可以的项目哦,要是所有逻辑都耦合在Controller里,那这个规划就不适用了。。。,所有逻辑都耦合在Controller里还做个

    1小时前
    00
  • 取消Win10开机系统选择倒计时,让电脑秒进系统

    取消Win10开机系统选择倒计时,让电脑秒进系统 近期,不少Win10用户反映在开机时会遇到一个选择系统的倒计时画面,这在一定程度上延缓了开机进程。对于追求高效启动体验的用户来说,这无疑是一个不必要的步骤。那么,如何取消这个倒计时,让电脑

    1小时前
    00
  • 2025年最受欢迎的10款免费CRM软件大对比

    在数字化转型浪潮下,越来越多的企业开始重视客户关系管理(CRM)系统。一个高效的CRM不仅能帮助企业理清客户脉络,还能提升销售效率、优化服务体验。2025年,市场上涌现了众多优秀的免费CRM软件,本文将为大家对比10款最受欢迎的产品,助您选

    1小时前
    00
  • 电脑密码在哪里设置win11,win11电脑开机密码怎么设置

    Win11系统由于许多设置和以前系统不一样了,所以很多用户们操作非常不习惯,有很多的小伙伴不知道win11系统怎么设置开机密码。给电脑设置密码,只有自己能打开进入系统桌面&a

    51分钟前
    00
  • 面试官:从三万英尺角度谈一下Ceph架构设计(1)

    把面试官当陪练,在找工作中才会越战越勇大家好我是小义同学,这是大厂面试拆解——项目实战系列的第3篇文章,如果有误,请指正。本文主要解决的一个问题,Ceph为例子 如何描述项目的架构。一句话描述:主要矛盾发生变化10年前的技术和方案,放到10

    43分钟前
    00
  • 非nvidia卡torchvision报错修复: operator torchvision::nms does not exist

    在Ascend 910b上安装vllm, 会自动把torchaudio和torchvision安装上去.安装前代码语言:shell复制pip list | grep torchtorch

    39分钟前
    00
  • AlignRAG:浙江大学提出的可泛化推理对齐框架,助力 RAG 系统解决推理失配问题

    近年来,检索增强生成(Retrieval-Augmented Generation, RAG)成为知识驱动文本生成的核心范式。然而,现有的 RAG 系统在推理过程中常常出现“推理失配”问题,即模型的推理路径与检索到的证据不一致,导致生成内容

    36分钟前
    00
  • 长读长测序揭示结直肠癌异常可变剪接图谱与新型治疗靶点

    徐州医科大学肿瘤研究所董东郑骏年教授团队在Genome Medicine杂志发表题为“Long-read sequencing reveals the landscape of aberrant alternative splicing

    34分钟前
    00
  • OWASP TOP10

    什么是OWASP?它的全称是 Open Web Application Security Project(开放式 Web 应用程序 安全 项目)TOP 10OWASP Top 10的意思就是10项最严重的Web 应用程序安全风险列表 ,它总

    31分钟前
    00
  • 开源在线考试系统

    看到调问已经开始扩展在线考试的场景,试了一下,发现在线考试的基本能力都已经支持了。主要是考试中的各种计分功能,包括对每道题的选项设置分值计算、考试时间限制等,和官方了解了一下,考试中的其他各项能力也在逐步完善,有需求可以随时

    25分钟前
    00
  • Go 语言 Mock 实践

    Mock 是软件测试中的一项关键技术,尤其在单元测试领域,可谓是“顶梁柱”般的存在,几乎不可或缺。它通过模拟真实对象的行为,使我们能在不依赖外部系统的情况下,专注测试代码的核心逻辑。对于测试开发、自动化测试,乃至性能测试中的某些场景,合理使

    22分钟前
    00
  • 3、win10重装系统后Mysql环境和数据的恢复

    因为电脑是机哥的原因,重装了好几次电脑,因为我习惯把软件都装在D盘。所以很多东西都还比较好恢复,在网上学会了怎么不卸载重装数据库,自己记录以备后面自己查

    19分钟前
    00
  • CUT&Tag 数据处理和分析教程(7)

    过滤某些项目可能需要对比对质量分数进行更严格的过滤。本文细讨论了bowtie如何分配质量分数,并举例说明。MAPQ(x) = -10 * log10log10(P(x is mapped wrongly)) = -10 * log10(p)

    17分钟前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信