1

I like to display php array in javascript, but in specific element with id. below is my code which shows what I want to do.

<?php function ogolne_informacje(){ 
    global $mypod;?>
        <strong><?php echo 'Pokój: '?></strong><?php echo $mypod->display('room')."<br />";?>
        <strong><?php echo 'Email: '?></strong><a href="mailto:<?php echo $mypod->display('user_email') ?>"><?php echo $mypod->display('user_email')."<br />";?></a>
        <strong><?php echo 'Telefon: '?></strong><?php echo $mypod->display('phone')."<br />"; }?>



<?php $i =0;
while ( $mypod->fetch() ) :{
    ob_start(); 
    ogolne_informacje();
    $output[$i] = ob_get_clean();
    $i++;
} endwhile;?>

<div id="test"></div>
<script>
var out = <?php echo $output; ?>;
    $(document).ready(function () {
    $('#test').html(out.toString());
});
</script>

How can I do that? Thanks!

Kapka
  • 17
  • 4
  • 1
    https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript – clearshot66 Jun 07 '17 at 15:34
  • @jeroen You might have picked the wrong duplicate. Above comment points out a better fitting one. – le_m Jun 07 '17 at 15:38
  • 1
    @le_m Could be, but the one-liner from the other one is exactly what the OP is looking for and seems to be discouraged by the first answer in the other question. Which I completely disagree with (not the whole answer, but discouraging using `json_encode()`, escaping is hard, etc.). – jeroen Jun 07 '17 at 15:44
  • 1
    @jeroen ah well, I was distracted by the linked question title referring to strings but the accepted answer fits indeed OP's needs. – le_m Jun 07 '17 at 15:54

1 Answers1

2

You can't loop like that, you need to loop the PHP array and push into javascript array:

<script type="text/javascript" language="javascript">
    var pausecontent = new Array();
    <?php while ( $mypod->fetch() ) :{ 
          ob_start();   
          ogolne_informacje();
    ?>
        pausecontent.push('<?php echo ob_get_clean(); ?>');
    <?php } ?>
</script>
Arun
  • 1,609
  • 1
  • 15
  • 18