简体   繁体   中英

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM