prestashop-使用ajax onclick创建新表单

prestashop-create new form with ajax onclick

本文关键字:表单 新表单 创建 使用 ajax onclick prestashop-      更新时间:2023-09-26

嗨,我是prestshop 1.6的新手,我使用jquery的$。调用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函数不返回任何值。请帮助我,我花了两天时间寻找解决方案

prestshop(前台)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,不确定是否可以使用helper创建表单,并使用Ajax显示它。我认为大多数模块只使用HTML