flobrflo Posted April 29, 2014 Share Posted April 29, 2014 (edited) Hi, in my module i would like to update some link in my block, so i call the same function than the begening but with ajax. i assign with smarty a token to use in tpl for build hrefs of my buttons. I try different way: $this->context->smarty->assign(array( 'token' => Tools::getAdminTokenLite('AdminModules'), )); //// or $this->context->smarty->assign(array( 'token' => Tools::getAdminToken('AdminModules'.(int)Tab::getIdFromClassName('AdminModules').(int)$this->context->employee->id)), )); //// or directly in tpl : {$smarty.get.token} the fact is, when i go to the page, the token is good no error, but when i call my page in ajax i got 2 error: Notice: Trying to get property of non-object in http://mydomain.com/classes/Tools.php on line 980 and the token is wrong. So when i call ajax, my content is update but links are wrong.. Thx for help =) EDIT : i remember $this don't work in ajax, so i get context like this: $context = Context::getContext(); //then (int)$context->employee->id but when i look at the context i see that employee is empty (that's why i can't get admin token) Any way to find it? Edited April 29, 2014 by flobrflo (see edit history) Link to comment Share on other sites More sharing options...
flobrflo Posted April 30, 2014 Author Share Posted April 30, 2014 (edited) I try to get id_employee with cookie, so i assign it like this : if(!$ajax = Tools::getValue('ajax', null)){ $this->context->cookie->__set('id_employee' , $this->context->employee->id); } That's ok i can see the id on my page. but when i call my function again with ajax i try: $context = Context::getContext(); d($context->cookie); and id_employee isn't in the cookie... like.. disappear. Edited April 30, 2014 by flobrflo (see edit history) Link to comment Share on other sites More sharing options...
skorupa Posted April 30, 2014 Share Posted April 30, 2014 Hi, I use this link generator in my smarty template: $.ajax('{$link->getAdminLink('AdminControllerName')}', { type: 'POST', ... }); And after render it looks like: $.ajax('index.php?controller=AdminControllerName=ce4d152fe19ee79c3ea65645b73ff52f', ... Link object can be acquired like this: $link = Context::getContext()->link; and then should added as variable in your template. Hope that will help you. Link to comment Share on other sites More sharing options...
flobrflo Posted April 30, 2014 Author Share Posted April 30, 2014 Thanks for your answer. =) I got the same problem, your link works when i arrived on the page. but when i reload with ajax the same error : Notice: Trying to get property of non-object in http://mydomain.com/classes/Tools.php on line 980 and it's seem that id_employee disappear again... Link to comment Share on other sites More sharing options...
skorupa Posted April 30, 2014 Share Posted April 30, 2014 Try adding ajax variable to your post data: $.ajax('{$link->getAdminLink('AdminControllerName')}', { type: 'POST', data: { ajax: 1, action: 'actionName', var1: 'data1', var2: 'data2' }, dataType: 'json', success: function(data, status, xhr){ /*...*/ }, error: function(data, status, xhr){ /*...*/ } } If it won't work what version of Prestashop you use, version of PHP, and if posibble enable xdebug and post stack trace. Link to comment Share on other sites More sharing options...
flobrflo Posted April 30, 2014 Author Share Posted April 30, 2014 I use Prestashop 1.6.0.5 with php 5.4 =) if fact, i would like to write html with the link admin. like: 1srt step : <a href="urladmin1?param=1">link</a><button onclick="update_url"> 2scd step : click on button and load: <a href="urladmin2?param=2">link</a><button onclick="update_url"> i can update the link but the urladmin2 is invalid. and this url is define in my php. (no choice ^^) for this i use a unique function : function initcontent (){ $context = Context::getcontext(); $href = "urltoload&token=".Tools::getAdminToken('AdminController'.(int)Tab::getIdFromClassName('AdminController').(int)$context->employee->id); //assign /// etc... } and this works at start but not when i reload, the function is called but only the link is false.. Link to comment Share on other sites More sharing options...
skorupa Posted April 30, 2014 Share Posted April 30, 2014 Hmm, it looks like employee is not set in context. If it was my code, I would comment out all thing that are unnecessary and check it out thene. Also check in FireBug what are contents of AJAX response that you get from server, after pressing button first time. Link to comment Share on other sites More sharing options...
flobrflo Posted April 30, 2014 Author Share Posted April 30, 2014 (edited) mmhh.. i actually try to minimalize my function for get only necessary action. it look like: my ajax : function reload_link(){ $.ajax({ url: '/modules/mymodule/ajax/ajax.php', type: "POST", data:"ajax=true&method=mymethode", dataType : "json", complete:function(data){ $('html').append(data.responseText); } }) } in ajax.php <?php include_once(dirname(__FILE__).'/../../../config/config.inc.php'); include_once(dirname(__FILE__).'/../../../init.php'); require_once(dirname(__FILE__).'/../mymodule.php'); switch (Tools::getValue('method')) { case 'mymethode' : $mymodule = new Mymodule(); die($mymodule->getContent()); break; } and in mymodule.php : public function getContent() { $context = Context::getcontext(); d($context->employee); } And it's the same thing... EDIT : thanks for your time !! =) Edited April 30, 2014 by flobrflo (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now