Jump to content

Proper way to create JavaScript object using PHP


NotionCommotion

Recommended Posts

I've flip-flopped on this topic for a while.  I need some data available to the client which is generated using PHP.  Below are two options to do so.  Is one preferred over the other?  Is there a better option?  Thank you

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Generating Serverside Generated JavaScript Object</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js" type="text/javascript"></script>
        <script>
            //Option 1: Generate the appropriate JavaScript directly using PHP
            var myJSONObj_1=<?php echo json_encode(array('a'=>1,'b'=>2,'c'=>3, 'd'=>array('x'=>1,'y'=>2,'z'=>3)));?>;
            $(function() {
                //Add all my JavaScript here...
                console.log('myJSONObj_1 test 1',myJSONObj_1);
            });
        </script>
        <script>
            $(function() {
                //Option 2: Server to just provide JSON
                $.getJSON( 'getMyJSON.php', function(myJSONObj_2) {
                    //Add all my JavaScript here...
                    console.log('myJSONObj_2 test 1',myJSONObj_2);
                });
            });
        </script>
        <script>
            //Some other imported script
            
            //No problem here
            console.log('myJSONObj_1 test 2',myJSONObj_1);
            
            //Opps, not so good, anything we could do?
            console.log('myJSONObj_2 test 2',myJSONObj_2);
            
        </script>
    </head>
    <body>
    </body>
</html>

getMyJSON.php

<?php
  header('Content-Type: application/json;');
  echo json_encode(array('a'=>1,'b'=>2,'c'=>3, 'd'=>array('x'=>1,'y'=>2,'z'=>3)));
?>
Link to comment
Share on other sites

depends on the application you're building.

Retrieving the json via ajax will entail an extra connection from each client, though useful if this information will be updated regularly via polling.

 

Having the json in the original php script will use less resources and will ensure the data is available to your javascripts when the page loads... though as above if you're updating the results via ajax anyway you may want to just retrieve via ajax... which would be be most logical for your application?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.