Assign a value to js and echo it anywhere on the same page

can someone point me in the right direction about what kind of function I need to use in order to pickup a dinamically generated php value at the bottom of a page and be able to echo this value on top of the page?

The following should work:

echo "<script>";
$htmlEncoded = htmlspecialchars($dynamicallyGeneratedPHPVariable, ENT_QUOTES);
echo "var phpVariable = '{$htmlEncoded}';";
echo "</script>";

You might need to contact a PHP expert in regards to how to make the PHP variable content safe and secure.

JavaScript code below the above JavaScript, can pick up phpVariable.

<script>
var p = document.createElement('p');
p.appendChild(document.createTextNode('The PHP variable is ' + phpVariable));
document.body.insertBefore(p, document.body.firstChild);
</script>

thank you Paul

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.