prestashop - 使用ajax onclick创建新表单

时间:2015-07-24 10:36:28

标签: 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函数什么都不返回。请帮助我,我花了两天时间寻找解决方案

1 个答案:

答案 0 :(得分:0)

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