简体   繁体   English

Ajax 在 Opencart 2 中调用 php 文件

[英]Ajax call a php file in Opencart 2

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.我想 ajax 调用我创建的系统/帮助程序的 php 文件,该文件调用在数据库中添加优惠券的模型方法。 the php file contains the following. php 文件包含以下内容。

<?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.我创建了一个 js 文件,它在提交表单时进行 ajax 调用。 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');最后我用 $this->document->addScript('catalog/view/javascript/test1.js') 在 catalog/controller/common/header.php 中调用了这个 js 文件;

The problem is that everytime i submit the form i get an error message from the ajax call.问题是每次我提交表单时,我都会从 ajax 调用中收到一条错误消息。 Can you tell me what i am doing wrong please?你能告诉我我做错了什么吗?

OpenCart doesn't allow call PHP files directly from system folder (check .htaccess file in system folder). OpenCart 不允许直接从系统文件夹调用 PHP 文件(检查系统文件夹中的 .htaccess 文件)。 Try to open https://yoursite/system/helper/acumba.php, You'll get 403 Forbidden.尝试打开 https://yoursite/system/helper/acumba.php,你会得到 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.您必须修改 /catalog/controller/extension/total/coupon.php 并放入您的方法,然后在您的 JS 文件中以这种方式调用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM