AJAX在OpenCart 2中调用PHP文件

发布于 2025-01-28 03:38:14 字数 1229 浏览 3 评论 0原文

我想调用我创建的系统/助手的PHP文件,该文件要求使用模型的方法,该模型在数据库中添加优惠券。 PHP文件包含以下内容。

<?php
function coupon_for_acumba() {
            $this->load->model('total/coupon');

            echo $this->model_total_coupon->coupon_test();
        }

我创建了一个JS文件,该文件在提交表单时进行AJAX调用。文件中的脚本是以下内容

let acumbaForm = document.querySelector('#form-acm_28955');
                acumbaForm.addEventListener('submit', function () {
                    setTimeout(function () {if (document.querySelector('.succes-alert-form-acm')) {
                        $.ajax({

                                url : '/system/helper/acumba.php',
                                type : 'POST',
                                success : function (result) {
                                console.log (result); // Here, you need to use response by PHP file.
                },
                    error : function () {
                        console.log ('error');
            }

                    });
                }}, 2000)
                    
                })

,最后我用$ this-&gt; document-&gt; addscript('catalog/view/view/javascript/test1.js');

问题是,每次我提交表格时,我都会从Ajax调用中收到错误消息。你能告诉我我做错了什么吗?

I want to ajax call a php file which i created system/helper which calls for a method of a model which adds a coupon in the database. the php file contains the following.

<?php
function coupon_for_acumba() {
            $this->load->model('total/coupon');

            echo $this->model_total_coupon->coupon_test();
        }

I created a js file which makes the ajax call when a form is submitted. The script in the file is the following

let acumbaForm = document.querySelector('#form-acm_28955');
                acumbaForm.addEventListener('submit', function () {
                    setTimeout(function () {if (document.querySelector('.succes-alert-form-acm')) {
                        $.ajax({

                                url : '/system/helper/acumba.php',
                                type : 'POST',
                                success : function (result) {
                                console.log (result); // Here, you need to use response by PHP file.
                },
                    error : function () {
                        console.log ('error');
            }

                    });
                }}, 2000)
                    
                })

finally i called this js file in catalog/controller/common/header.php with $this->document->addScript('catalog/view/javascript/test1.js');

The problem is that everytime i submit the form i get an error message from the ajax call. Can you tell me what i am doing wrong please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无风消散 2025-02-04 03:38:14

OpenCart不允许直接从System文件夹拨打PHP文件(在System Folder中查看.htaccess文件)。尝试打开https:// yoursite/system/helper/acumba.php,您将被禁止403。您必须使用路由来调用方法。

url : '/system/helper/acumba.php', // wrong

您必须修改/catalog/controller/extension/total/coupon.php,然后使用您的方法,然后在JS文件中使用此方式。

url : 'index.php?route=extension/total/coupon/coupon_for_acumba',

OpenCart doesn't allow call PHP files directly from system folder (check .htaccess file in system folder). Try to open https://yoursite/system/helper/acumba.php, You'll get 403 Forbidden. You have to use a route to call a method.

url : '/system/helper/acumba.php', // wrong

You have to modify /catalog/controller/extension/total/coupon.php and put in your method, then call this way in your JS file.

url : 'index.php?route=extension/total/coupon/coupon_for_acumba',
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文