Mamun Sardar
Mamun Sardar

Reputation: 2729

Sharing data between php and javascript on same page

I am trying to share data on same .php page. I receive some data on a page via POST method and I want to use it on javascript on that page during page load. But somehow javascript is showing the value undefined. What's the reason and how do I fix it?

<?php
    echo "<label id='origin' style='visibility:hidden;'>".$_POST["startStation"]."</label>";
?>
<script type="text/javascript">
    alert(document.getElementById('origin').value);
</script>

Upvotes: 0

Views: 262

Answers (1)

rationalboss
rationalboss

Reputation: 5389

Because label does not have a .value. To access that data you need to use .innerHTML.

<?php echo "<label id='origin' style='visibility:hidden;'>".$_POST["startStation"]."</label>"; ?>
<script type="text/javascript"> alert(document.getElementById('origin').innerHTML); </script>

Upvotes: 3

Related Questions