Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Output Javascript with PHP

    Hello,

    I am currently trying to implement the Nokia Maps API in my website and I am having trouble dynamically outputting javascript with PHP.

    Assuming that I am correct that when a browser opens a website the browser parses the html string and implements the code, including javascript scripts.
    So theoretically it doesen't matter if I hardcode it in the HTML or if I "echo" it in PHP!?

    This is the hardcoded html:
    Code:
    <div id="map"></div>
        <script type="text/javascript">
    	nokia.maps.util.ApplicationContext.set({"appId": "*****", "authenticationToken": "*****"});
        var map = new nokia.maps.map.Display(
            document.getElementById("map"), {
                components: [ 
                    // Behavior collection
                    new nokia.maps.map.component.Behavior(),
                    new nokia.maps.map.component.ZoomBar(),
                    new nokia.maps.map.component.Overview(),
                    new nokia.maps.map.component.TypeSelector(),
                    new nokia.maps.map.component.ScaleBar() ],
                zoomLevel: 10,
                center: [52.51, 13.4]
            });
          
        </script>
    and this is the php code to echo the exact same code:
    Code:
    echo '<div id="map"></div>
        <script type="text/javascript">
    	nokia.maps.util.ApplicationContext.set({"appId": "*****", "authenticationToken": "*****"});
        var map = new nokia.maps.map.Display(
            document.getElementById("map"), {
                components: [ 
                    // Behavior collection
                    new nokia.maps.map.component.Behavior(),
                    new nokia.maps.map.component.ZoomBar(),
                    new nokia.maps.map.component.Overview(),
                    new nokia.maps.map.component.TypeSelector(),
                    new nokia.maps.map.component.ScaleBar() ],
                zoomLevel: 10,
                center: [52.51, 13.4]
            });
          map.removeComponent(map.getComponentById("zoom.MouseWheel"));	  
        </script>';
    This is the actual HTML Output of the php script:
    Code:
    <div id="map"></div>
        <script type="text/javascript">
    	nokia.maps.util.ApplicationContext.set({"appId": "*****", "authenticationToken": "*****"});
        var map = new nokia.maps.map.Display(
            document.getElementById("map"), {
                components: [ 
                    // Behavior collection
                    new nokia.maps.map.component.Behavior(),
                    new nokia.maps.map.component.ZoomBar(),
                    new nokia.maps.map.component.Overview(),
                    new nokia.maps.map.component.TypeSelector(),
                    new nokia.maps.map.component.ScaleBar() ],
                zoomLevel: 10,
                center: [52.51, 13.4]
            });
          map.removeComponent(map.getComponentById("zoom.MouseWheel"));	  
        </script>
    Adding
    Code:
    document.write("Hello World");
    to the script displayed the text "Hello World", so why isn't the rest executing?

    Thanks in advance

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Worcester
    Posts
    172
    Thanks
    13
    Thanked 6 Times in 6 Posts
    I sometimes find when creating js in php any line breaks get added too, and this causes a js error.

    Try having the php string all on one line and give that a shot, might help?

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,304
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Use a heredoc instead:
    Code:
    print <<<STOP
    <div id="map"></div>
        <script type="text/javascript">
    	nokia.maps.util.ApplicationContext.set({"appId": "*****", "authenticationToken": "*****"});
        var map = new nokia.maps.map.Display(
            document.getElementById("map"), {
                components: [ 
                    // Behavior collection
                    new nokia.maps.map.component.Behavior(),
                    new nokia.maps.map.component.ZoomBar(),
                    new nokia.maps.map.component.Overview(),
                    new nokia.maps.map.component.TypeSelector(),
                    new nokia.maps.map.component.ScaleBar() ],
                zoomLevel: 10,
                center: [52.51, 13.4]
            });
          map.removeComponent(map.getComponentById("zoom.MouseWheel"));	  
        </script>
    STOP;
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •