PHP include withing javascript

Discussion in 'PHP' started by tanvirtonu, Jun 15, 2009.

  1. #1
    Can I include a php file from within javascript and get a return variable from that php.I mean-
    <script type="text/javascript">
    
    var data =   <?php include('getData.php') ?>
    
    </script>
    Code (markup):
    Can anybody tell me how to do the above thing?
     
    tanvirtonu, Jun 15, 2009 IP
  2. webwurks

    webwurks Well-Known Member

    Messages:
    126
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #2
    As long as the example code you have in your post is in a PHP file it should work.
    Although, the way I have done something similar is to use PRINT to print out the Javascript portion inside a PHP block in a PHP file. For example:

    print('<script type="text/javascript">');
    print('var data = ');

    ** print out your data you were getting from getData.php **
    print('</script>');
     
    webwurks, Jun 15, 2009 IP
  3. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #3
    yes its possible to do that
     
    Bohra, Jun 16, 2009 IP
  4. Sudoku-Master

    Sudoku-Master Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you have to know that php runs on the server to generate a page, and js on the browser, so js starts after php ends... you can generate js dynamic over php, but you couldn't generate php dynamic over js...
     
    Sudoku-Master, Jun 16, 2009 IP
  5. bluebenz

    bluebenz Well-Known Member

    Messages:
    876
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    138
    #5
    in short, create the js text script in php using print or echo syntaks.
    :)
     
    bluebenz, Jun 16, 2009 IP
  6. livedating

    livedating Active Member

    Messages:
    161
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    83