Reputation: 11
How is made a ajax call in a new page called by a controller in prestashop. Example:
-i have a form page call formulario.php in root folder -this page call FormularioController.php in controller folder and shows me perfect the tpl and the page, the tpal is in themes/default folder
but here is the issue when i make an Ajax call for checking the form and load the errors or the result in a div it put all the page again inside that div not only the response.php that i call.
Any ideas how to fix this , or a guide to make a ajax call from a page. the doc of prestashop about ajax is not clear
Thank you
Upvotes: 1
Views: 2075
Reputation: 33
Did you find this page? http://doc.prestashop.com/display/PS15/Using+jQuery+and+Ajax#UsingjQueryandAjax-MakingAjaxcallswithjQuery
I used it to develop my module. It could also help you to take a lot to other module which use ajax call (can't remember which ones does).
Here's how you can do your ajax call :
var query = $.ajax({
type: 'POST',
url: baseDir + 'modules/mymodule/ajax.php',
data: 'method=myMethod&id_data=' + $('#id_data').val(),
dataType: 'json',
success: function(json) {
// ....
}
});
And then, you just need to create the PHP file. Using this way, Prestashop core won't be loaded, so you will have to do it manually if you want to use Prestashop function :
// HTTP headers for no cache etc
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
require_once(dirname(__FILE__).'/../../config/config.inc.php');
require_once(dirname(__FILE__).'/../../init.php');
To generate JSON output, you can use this :
die(Tools::jsonEncode($myArrays));
Hope that you got what you need.
Upvotes: 1