Nullified Posted February 22, 2015 Share Posted February 22, 2015 I am attempting to declare a global smarty variable from within a custom page, but I find that "$ca->assign('var', $_POST['var']);" only makes $var available according to the template set with $ca->setTemplate('template'); I need the variable to be globally available on all templates. The only other way I know of how to do this is by returning an array in the ClientAreaPage hook, however this will not work because I need to post the value of the variable with JavaScript (json) to the php script, then use the posted variable as the smarty global variable. MY CODE Called in header.tpl: function saveResolution() { $.post('resolution.php', {width:screen.width}, function(json) { if(json.outcome == 'success') {location.reload();} },'json'); } Yes, this function reloads the page on success, but doesn't loop because the function is only called if $screenwidth is not set or has changed. resolution.php code: <?php define("CLIENTAREA",true); require("init.php"); $ca = new WHMCS_ClientArea(); $ca->setPageTitle("Screen Resolution"); $ca->addToBreadCrumb('index.php',$whmcs->get_lang('globalsystemname')); $ca->addToBreadCrumb('resolution.php','Screen Resolution'); $ca->initPage(); $ca->assign('screenwidth', $_POST['width']); $_SESSION['screenwidth'] = $_POST['width']; define('screenwidth', $_POST['width']); $ca->setTemplate('resolution'); // I have tried setting this to header echo json_encode(array('outcome' => 'success', 'width' => $_POST['width'])); ?> No matter what I do I am unable to access the JavaScript posted screen.width via {$smarty.const.screenwidth}, {$smarty.session.screenwidth} or {$screenwidth} 0 Quote Link to comment Share on other sites More sharing options...
sentq Posted February 22, 2015 Share Posted February 22, 2015 where is the rest of your Javascript code it that all of it? 0 Quote Link to comment Share on other sites More sharing options...
Nullified Posted February 22, 2015 Author Share Posted February 22, 2015 (edited) That is all that is relevant. It's a jQuery function (json) that posts screen.width to the php script resolution.php (which is a custom whmcs page (where I am trying to declare the value of screen.width (as $_POST['width']) as a global smarty variable which can be called by any template. The issue is not with the javascript as I can recall the value of the posted variable back in the js if statement that verifies the success of the post. The issue is with the declaration of the global smarty variable from my custom page. Edited February 22, 2015 by Nullified 0 Quote Link to comment Share on other sites More sharing options...
sentq Posted February 22, 2015 Share Posted February 22, 2015 i made few changes to your code to work properly, just add the following code inside resolution.php or new file if you need, this file will be responsible to save the new screenwidth value in $_SESSION so you'll be able to access this value from within PHP and TPL files inside WHMCS,,, <?php define("CLIENTAREA",true); require("init.php"); $width = intval($_POST['width']); $defaultWidth = '0'; # Update ScreenWidth if ( isset($_POST['width']) && $width!='0' ){ $_SESSION['screenwidth'] = $width; } # Set Default ScreenWidth else { if (!isset($_SESSION['screenwidth'])){ $_SESSION['screenwidth'] = $defaultWidth; } } echo json_encode( array('outcome' => 'success', 'width' => $width) ); ?> from any .tpl file inside call it in this way {$smarty.session.screenwidth} I didn't made tests to your JS code, you may need to monitor it's actions by FireFox/Chrome console 0 Quote Link to comment Share on other sites More sharing options...
Nullified Posted February 23, 2015 Author Share Posted February 23, 2015 The session and constant vars declared in custom pages are not accessible on actual whcms pages and vice versa. Hence you'll see in my code that I have already tried to do this. I am almost giving up on this....it's ridiculous. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.