javascript - prestashop-使用 ajax onclick 创建新表单

标签 javascript php jquery ajax prestashop

您好,我是 prestashop 1.6 的新手,我使用 jquery 的 $.ajax 来调用 PHP 脚本。我想要的是在单击按钮时创建新表单,这是我的代码:

在 catproduct.php 中:

public function clickme(){

$html = '<a href="#" class="displayForm">Click me</a>
         <div id="myFormm"></div>
        <script type="text/javascript">
        $( ".displayForm" ).click(function() {
            $.ajax({
              url: "'._MODULE_DIR_.$this->name.'/ajax.php",
              context: document.body,
                })
                .done(function(data) {
                $( "#myFormm" ).html( data );
                alert( "Load was performed." );
            });


        })
    </script>';



return $html;

}


public function renderForm()
{
    $fields_form = array(
        'form' => array(
            'legend' => array(
                'title' => $this->l('Add new category'),
                'icon' => 'icon-cogs'
            ),
            'input' => array(
                array(
                    'type' => 'text',
                    'label' => $this->l('FCI'),
                    'name' => 'FCI',
            ),
            ),

            'submit' => array(
                'title' => $this->l('Save')
            )
        ),
    );
    $helper = new HelperForm();
    $helper->show_toolbar = false;
    $helper->table = $this->table;
    $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
    $helper->default_form_language = $lang->id;
    $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
    $this->fields_form = array();

    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submitCatForm';
    $helper->fields_value['FCI'] = 'ddddd';
    $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='
    .$this->tab.'&module_name='.$this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');

    return $helper->generateForm(array($fields_form));
}

ajax.php文件(ajax文件调用renderForm()函数创建新表单):

require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');

include_once('catproduct.php'); 
$module = new catproduct(); 
echo $module->renderForm();

done 函数不返回任何内容。请帮助我,我花了两天时间寻找解决方案

最佳答案

Prestashop(前台)中的 ajax 调用示例:

$( document ).ready(function() {
        var form = $('.MyForm');
          form.submit(function(e) {
                $.ajax({
                    type: 'POST',
                    url: '../modules/MyModule/controllers/front/MyFrontController.php',
                    data: $(this).serialize(),
                    dataType: 'json',
                    success: function(jsonData) {
                        console.log(jsonData);
                    }
                });
                e.preventDefault();
      });
    });

控制台显示结果。如果不是,则php文件有问题。

这是 PHP 文件的示例:

<?php

include(dirname(__FILE__).'/../../../../config/config.inc.php');
include(dirname(__FILE__).'/../../../../init.php');
include(dirname(__FILE__).'/../../classes/MyUsefulModuleClass.php');
if (Tools::getIsset('my_useful_id')) { 
        echo Tools::jsonEncode('Ajax return fine'));       }?>

检查是否调用了 config.inc.php 和 init.php。在处理结果时始终 console.log()。

然后尝试单独创建表单,只能使用HTML,不确定是否可以使用帮助器创建表单,并使用Ajax 显示。我认为大多数模块只使用 HTML

关于javascript - prestashop-使用 ajax onclick 创建新表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31608325/

相关文章:

javascript - 设置环境变量 grunt 或 grunt-exec

javascript - Select 并不总是与 AngularJS 中的初始模型值匹配

php - 验证表单后禁用 Bootstrap 验证器

jquery - 在 Chrome 扩展中重写的新选项卡中使用 jQuery 违反了内容安全策略?

jquery - 在附加元素中查找目标以调用单击事件

javascript - 如何使用 native Angular 代码解析外部链接 url?

php - 帮助从 MySQL 数据库中获取文本到下拉菜单中

javascript - 我如何使 2 个函数在 jquery 中与 "or"同时工作?

jquery - 在 jQuery 中循环遍历数组

javascript - 如何从 chrome 调试 API 中捕获时间线数据