0

I've got a variable in a js script, which I want to set from a php script and access from my js script.

How can I do this in good coding style manner.

At the moment I use a global variable in the js script for that purpose :(

Setting the variable from PHP:

$js = '<script type="text/javascript">';
$js .= "g_sourceType = '$sourcetype';";
$js .= "</script>\n";

Defining and accessing it in my js script:

var g_sourceType; // at global scope

...

function SetSubmitFocus()
{
  switch (g_sourceType) {
...
Christian Graf
  • 406
  • 2
  • 10
  • 26

1 Answers1

0

Check If this might help

$js = '<script type="text/javascript">';
$js .= "(function() {
        g_sourceType = function() {
        return '$sourcetype';
        };   
        })();";
$js .= "</script>\n";
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87