1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Include file contents with javascript

Discussion in 'JavaScript' started by MrLeN, Dec 11, 2012.

  1. #1
    I have a page called: file.php

    I also have a file called: javascript.js

    Inside javascript.js I want to include the contents of file.php

    ie: I want javascript.js to look up file.php, get the contents out of it and then output the contents.

    Is this possible?

    If so, can you enlighten me anhy further?
     
    MrLeN, Dec 11, 2012 IP
  2. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #2
    I worked it out:

    1). Put this in the header:
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    
    Code (markup):
    2). This will display the file:
    
    $.get("text.txt", function(data){
      document.write("Data Loaded: " + data);
    });
    
    Code (markup):
     
    MrLeN, Dec 11, 2012 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    It would have been much easier to put your Javascript code in your PHP file. That's how it's normally done.
     
    Rukbat, Dec 12, 2012 IP
  4. udores

    udores Member

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    Though i would agree with you that you have a better idea of what you want to achieve, i must say this method is so "not-professional" - precisely because the document.write method simple makes the content of the file useable only to your client browser and NOT to your own code.

    A better way would be to use a PHP file to include your javascript source code. Then after retrieving the requested file, you can store the contents of the file in a string, manipulate it anyway you like before sending it to the user browser.
     
    udores, Dec 13, 2012 IP
  5. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #5
    ignore - I was trippin' - thought I was replying to a different post
     
    Last edited: Dec 13, 2012
    MrLeN, Dec 13, 2012 IP
  6. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #6
    ignore - I was trippin' - thought I was replying to a different post
     
    MrLeN, Dec 13, 2012 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #7
    I'm responding to the post you deleted because you have to learn how the web actually works, or you'll keep making mistakes like the one you did.
    PHP runs on the server, creating the HTML, CSS and Javascript (and accessing the database and anything else it needs to do to get the data it needs to build the page). Then the page is sent to the user, and Javascript runs - in the browser.

    The only way to "send" a Javascript variable to PHP is by using AJAX. Then the PHP code (not the same code that generated the page, although it can be in the same file) runs, using the variable(s) Javascript sent it, and prints its output to the browser, where Javascript gets it and does something with it (usually displaying it on the user's screen).

    Javascript doesn't make session variables - those exist on the server (where Javascript doesn't run).

    You have two separate computers, running two separate programs. PHP can use its variables to build HTML or Javascript statements. Javascript can communicate with PHP by using AJAX. The two aren't running at the same time, not do they "share" variables.

    Once you have that firmly embedded in your subconscious (it takes some people seconds, it takes some people months), you'll never make the "embed PHP in Javascript" mistake again, and you'll understand why embedding Javascript in PHP is trivial.

    Oh, and cookies are strictly Javascript. You can write the Javascript in PHP as echo statements, or you can close the PHP and just write <script> and start writing Javascript code, but the cookies are handled by Javascript running in the browser.
     
    Rukbat, Dec 14, 2012 IP
  8. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #8
    I deleted the response because I thought I was replying to something else. So the response I gave had nothing to do with this post.
     
    MrLeN, Dec 14, 2012 IP
  9. udores

    udores Member

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #9
    You are 100% correct when you say php processing ends on the server and javascript runs only on the browser except via ajax., But the notion that embeding PHP in javascript is a mistake and doing it vice versa is trivial, is easily misconceivable.

    Firstly, if you are to use PHP code in any file at all the file must have the .php extension so unarguably you CANNOT embed php code in a javascript .js file, however, it is very possible to embed php code within javascript code. Take for instance this simple REDIRECT javascript code:

    .....
    $location = "http://yahoo.com";
    ?>
    <script>
    window.location = "<?php echo $location; ?>"
    </script>
    <?php....

    Also, on the otherhand embeding javascript into php is as simple as inserting the javascript source code in a php string and echoing it on the server before sending it to the client. So, it can be done both ways.
     
    udores, Dec 28, 2012 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    No, it isn't -- your example even fails to do so because that PHP is run SERVER SIDE -- that echo and <?php ?> isn't sent to the client, and isn't run on the client. You may be BUILDING the javascript with PHP, but that is NOT running in the code. There's a difference.

    NOT that I'd be opening and closing PHP every time I want output like that -- but if I had my way <?php and ?> would be removed from the language.
     
    deathshadow, Dec 28, 2012 IP