Jump to content

Declaring a global variable in custom page


Nullified

Recommended Posts

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}

Link to comment
Share on other sites

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 by Nullified
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated