I am very new to javascript. before asking this question I ran through the obvious steps of passing a varialbe from PHP into js. I can pass the php var into a js var and output that using window.alert easily and the variable does show up there. But I must be trying to put this into a type of script that isn't the easiest. The script I am using is actually a Single Platform script that shows a menu.
<script type="text/javascript" src="http://menus.singleplatform.co/businesses/storefront/?apiKey=ke09z8icq4xu8uiiccighy1bw"></script>
<script>
var options = {};
options['PrimaryBackgroundColor'] = '#d9d9d9';
options['MenuDescBackgroundColor'] = '#d9d9d9';
options['SectionTitleBackgroundColor'] = '#f1f1f1';
options['SectionDescBackgroundColor'] = '#f1f1f1';
options['ItemBackgroundColor'] = '#ffffff';
options['PrimaryFontFamily'] = 'Roboto';
options['BaseFontSize'] = '15px';
options['FontCasing'] = 'Default';
options['PrimaryFontColor'] = '#000000';
options['MenuDescFontColor'] = '#000000';
options['SectionTitleFontColor'] = '#555555';
options['SectionDescFontColor'] = '#555555';
options['ItemTitleFontColor'] = '#555555';
options['FeedbackFontColor'] = '#555555';
options['ItemDescFontColor'] = '#555555';
options['ItemPriceFontColor'] = '#555555';
options['HideDisplayOptionPhotos'] = 'true';
options['HideDisplayOptionDisclaimer'] = 'true';
options['MenuTemplate'] = '2';
//options['MenuDropDownBackgroundColor'] = '#f1f1f1';
options['MenuIframe'] = 'false';
new BusinessView("restaurantName", "menusContainer", options);
</script>
<script type="application/javascript">
$(document).ready(function() {
var interval = setInterval(function() {
if ($('#sp_main').length > 0) {
$('#menusContainer').after('<div class="more-link-container"><a class="more" >More <i class="fa fa-caret-down"></i></a><a class="less">Less <i class="fa fa-caret-up"></i></a></div>');
$('.more-link-container .less').hide();
clearInterval(interval);
}
}, 50);
});
</script>
I have to switch out the text that is shown at new BusinessView "restaurantName"
right at the end of the first major script.
The value is coming from $_POST using PHP and I need to put that value into that section. I tried printing out the js var directly in that line and it breaks. I tried echoing out the php variable using php and it breaks. Should I instead redo this code so that a variable is being passed into this javascript class (I assume it's a class? I'm too new into JS to be intelligent on that). Any help would be greatly appreciated.
So here is what I've done:
I created a simple js var to php var and window alert that to the screen. This works as expected:
<script type="text/javascript">
rest_singlemenujs = <?php echo json_encode($rest_singlemenu); ?>;
window.alert(rest_singlemenujs);
</script>
And when I update the script to show this:
new BusinessView("rest_singlemenujs", "menusContainer", options);
It just shows the text I typed in and not actually passing the variable. So next step is maybe it should be an entire string with the variable in the middle so I do this:
document.write("new BusinessView("' + rest_singlemenujs + '", "menuContainer", options);";
And that just stops the script from executed at the new BusinessView("
I then try this line with () around the var name but the var is not being passed:
new BusinessView("(rest_singlemenujs)", "menusContainer", options);