Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 558 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call a prestashop module with ajax

#1
I have a PrestaShop module called 'MyMenu' and I want call this menu with an AJAX call.
My module is displayed in the `hookFooter()` method:

public function hookFooter()
{
$display = $this->display(__FILE__, 'megamenu.tpl', $smartyCacheId);
Tools::restoreCacheSettings();
return $display;
}

I want display with this script:

<div class="load_menu"></div>
<script>
$(document).ready(function (e) {
$.ajax({
method: "POST",
url: "../modules/MyMenu.php",
data: {},
success: function (data) {
$('.load_menu').html(data);
}
})
});
</script>
Reply

#2
If you don't want to pass the url by the module, the js snippet should be like this.

$(document).ready(function(){
$.ajax({
type: "POST",
headers: { "cache-control": "no-cache" },
url : baseDir + 'modules/yourmodulename/yourfile.php',
data: {
token : token
},
success : function(data){
$('.load-menu').html(data)
}
});
});

Where `yourmodulename` is the name of your module and `yourfile.php` is the code where you retrieve the menu.

Don't forget to add to your data the token, it's to prevent a CSFR attack, obviously you have to check the token in your server side script as well.
Reply

#3
The best way is to do it via a front controller linked to your module.
You can call the url like this :

$link->getModuleLink('modulename','controller', $parameters);
// Parameters is an optionnal array, it can be empty

And for the controller, place a file like this ./modules/modulename/controllers/front/ajax.php with this kind of content :



class ModuleNameAjaxModuleFrontController extends ModuleFrontController
{
public function initContent()
{

$response = array('status' => false);

require_once _PS_MODULE_DIR_.'modulename/modulename.php';

$module = new ModuleName;

if (Tools::isSubmit('action')) {
$context = Context::getContext();

$cart = $context->cart;

switch (Tools::getValue('action')) {

case 'actionname':


$response = array('status' => true);

break;

default:
break;

}
}

// Classic json response
$json = Tools::jsonEncode($response);
$this->ajaxDie($json);

// For displaying like any other use this method to assign and display your template placed in modules/modulename/views/template/front/...
// $this->context->smarty->assign(array('var1'=>'value1'));
// $this->setTemplate('template.tpl');

// For sending a template in ajax use this method
// $this->context->smarty->fetch('template.tpl');

}
}
Reply

#4
In a new file at the module root, you can create a file "ajax.php"

require_once(MODULE_DIR.'MyMenu/mymenu.php');
if(Tools::getValue('token') !=
$mymenu = Module::getInstanceByName('mymenu');
$menu = $mymenu->hookFooter();
die($menu);

In your js, at the root of your module

<script>
$(document).ready(function (e) {
$.ajax({
method: "POST",
url: "./ajax.php",
data: {},
success: function (data) {
$('.load_menu').html(data);
}
})
});
</script>




Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through