I need to share some Global Variables across several HTML and PHP pages. I created a small HTML file that contains a series of hidden fields that contains several lines like this:
<input type="hidden" name="pageColor" id="pageColor" value="#DCE6F1">
<input type="hidden" name="bgColor" id="bgColor" value="#004C00">
<input type="hidden" name="LamCost" id="LamCost" value="3">
The form has about 7 of these fields. For each page that needs this data, I have created an INCLUDE statement that adds the form with the hidden fields near the top of the page:
<!---------[ Script required for includes put in <head>]------------------->
<script language="JavaScript">
function processUser()
{
var parameters = location.search.substring(1).split("&");
var temp = parameters[0].split("=");
l = unescape(temp[1]);
temp = parameters[1].split("=");
p = unescape(temp[1]);
document.getElementById("log").innerHTML = l;
document.getElementById("pass").innerHTML = p;
}
</script>
<!----------[ End of INLUDE script ]--------------->
<!-------[Near the topy of the <BODY> add this INCLUDE ]--------------->
<!--[ Import common VBLS ]---------------->
<div id="cData-placeholder"></div>
<script>
$(function(){
$("#cData-placeholder").load("includes/commonData.html");
});
</script>
<!--[ end of Common VBLS ]--------------------------->
The problem I am having is NO success at using those variables in other parts of the page. I have tried javascript & PHP:
var savLamCost = "<?php $_REQUEST['LamCost'] ?>";
var savLamCost = document.getElementById("LamCost").value;
...and in PHP:
$savLamCost = $_REQUEST["LamCost"];
$savLamCost = $_POST["LamCost"] ;
I would appreciate any suggestions of better ways to share global variables across several pages (the goal is to make future changes easy, by editing just one location to update all variables).
My thanks in advance!
Define/create a global.js:
var global_pageColor = "#DCE6F1";
var global_bgColor = "#004C00";
var global_bgColor = "3";
Call it in your other files, always being the first linked JS file in the html's head:
<html>
<head>
<!-- global.js -->
<script src="<path>/global.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>
Because the "include" statements failed &/or worked inconsistently, instead of a .js file, I am storing all shared data in a mySQL DB. Each page that needs one or more of those variables gets them with a mySQL SELECT statement. THIS IS WORKING VERY WELL FOR ME... it may be over-kill, but it works consistently and reliably.