Show value $ _SESSION with JavaScript


I'm painting HTML%% of PHP and I need to handle it with JavaScript.


<p id="elem" value=" <?php echo $_SESSION['variable']; ?> "></p>

The result of this operation is:

<p id="elem" value="0"></p>

For the time being, it is the expected result.

The problem is that when wanting to handle the new $_SESSION['variable'] with JavaScript, it returns an error value="0" .


>> undefined
asked by Pablo García 13.04.2016 в 20:59

2 answers


If your intention is to call that attribute, do it: elem.getAttribute ("value");

But under when you create control, if you do it before the control is rendered it will still give undefined

EDIT: I keep my answer, for either of the two cases.

answered by 13.04.2016 / 21:06

First of all, remember that PHP runs On server and Javascript On client . Taking this into account and the natural process of a 'rendering of the pages' we can see that (in a very very shallow way) PHP - > HTML - > Javascript

Therefore, every time you prepare your <?php echo $_SESSION['variable']; , this already comes to the ready and ready browser, so that you can now execute your Javascript code.

Following your example, I have <p id="elem" value="0"></p> , so, to capture that use value (using Jquery)

$(function () {    
    var valueQueNecesito = $('#elem').val();

Javascript (pretty basic)

var value = document.getElementById(elem).value;


answered by 14.04.2016 в 17:39