0

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&nbsp;<i class="fa fa-caret-down"></i></a><a class="less">Less&nbsp;<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);
Zach Smith
  • 5,490
  • 26
  • 84
  • 139
  • i read same thing on SO. I did point 3 http://stackoverflow.com/a/23740549/151438 but when i put the var into the new BusinessView the entire thing breaks. – Zach Smith Feb 08 '16 at 20:37
  • 1
    "The entire thing breaks" doesn't really demonstrate or even describe the problem. If what you're doing is somehow different from what that question addresses, show the code and describe the problem. – David Feb 08 '16 at 20:38
  • makes sense. i've added the first two things that the SO post you referenced tells me to do – Zach Smith Feb 08 '16 at 20:44
  • `"It just shows the text I typed in and not actually passing the variable."` - That's because you're not using the variable. You're just using a string which happens to contain the same text as the name of the variable. Use the variable itself if that's the intent. – David Feb 08 '16 at 20:45
  • i tried that and still same results. is there another way to print out the var other than using (var)? – Zach Smith Feb 08 '16 at 20:50
  • Why do you keep using strings? This is a string: `"rest_singlemenujs"` and this is a variable: `rest_singlemenujs` I don't know where you're getting this string interpolation syntax but JavaScript has never had that. – David Feb 08 '16 at 20:51
  • Because this is the original script: new BusinessView("restaurantName", "menusContainer", options);. I didn't write it the third party people at open table did. I'm trying to just pass a string variable into the "VALUE" of "restaurantName". I am forced to pass this inside the "" for this script to work. That is the entire point of this post. I've tried three well known uses and it isn't working so this has to be something bigger than just passing a php var to a js var. I've done that already. I'm trying to print the value inside this "" script I referenced in my post. – Zach Smith Feb 08 '16 at 20:55
  • What you're describing doesn't make any sense. If you have a variable called `rest_singlemenujs` and you want to pass it to a function, then pass it to the function: `new BusinessView(rest_singlemenujs, "menusContainer", options);` You keep wrapping it in quotes, which makes it a *string literal* and not the variable you defined. – David Feb 08 '16 at 20:57
  • @David you are VERY smart. this is what i was missing. this makes perfect sense. great job! i really appreciate this. i was getting the concept of passing the var but didn't realize that was a function and I could just pass the var into it. VERY EASY thanks to King David! – Zach Smith Feb 08 '16 at 20:59

0 Answers0