...">

Transferring data from PHP to JavaScript

I have a php variable called $VrHistorySlider defined as follows:

 $VrHistorySlider.='<table border="0"><tr><td>(oldest)</td><td style="padding:0px 10px 0px 10px ;">&nbsp;&nbsp;<div id="note-slider-'.$VrNoteId.'" style="width:'.$VrSliderWidth.'px;background: url(\''.$VrSliderBGImg.'\') repeat-x ;" class="ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all"></div>&nbsp;&nbsp;</td><td>(latest)</td></tr></table>'; 

Now I want to save it in a JavaScript variable. I tried this:

 var slidercontent = <?= $VrHistorySlider ?>; 

but he gives me this error:

unknown nbsp xml object
<parent xmlns=""><table border="0"><tr...td>(latest)</td></tr></table></parent>

If this is not the case, then how do I assign this $VrHistorySlider a JavaScript slidercontent variable?

+4
source share
2 answers
 var slidercontent = <?php echo json_encode($VrHistorySlider); ?>; 
+2
source

All you need is quotes around the php variable, because when you try to assign it so that it puts the raw code on the page, rather than making it a string.

You also need to avoid quotation marks in the string, as otherwise they will appear to end in the string and it will not work.

So it should look like this:

 var slidercontent = "<?=addslashes($VrHistorySlider)?>"; 
0
source

Source: https://habr.com/ru/post/1379077/


All Articles