inserting php code in javascript code

Discussion in 'PHP' started by hulkrana, Oct 1, 2007.

  1. #1
    hello

    can anyone tel me that either we can insert php code in javascript code for dynamic menu submenu creation .

    plz advice me ;
    thx
     
    hulkrana, Oct 1, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Save the javascript file with a .php extension, and add this line at the top of it.

    
    header('Content-Type: text/javascript');
    
    PHP:
    Then include the file like it was a normal javascript file.

    
    <script type="text/javascript" src="file.php"></script>
    
    Code (javascript):
     
    nico_swd, Oct 1, 2007 IP
  3. sdemidko

    sdemidko Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #3
    also you can insert special .htaccess instructions to your folder with js files
    there you will specify that apache need first to parse some php
     
    sdemidko, Oct 2, 2007 IP
  4. camp185

    camp185 Well-Known Member

    Messages:
    1,653
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    180
    #4
    Are you thinking like this: <script><? echo $tring; ?> </script>

    If so maybe think like this <? $javacode = "<script>$tring</script>"; ?>
     
    camp185, Oct 2, 2007 IP
  5. codyrockx

    codyrockx Peon

    Messages:
    33
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The cleanest way would be to add this code to the .htaccess file containing your javascript file. This will tell apache to parse any file ending with .js as php before outputting.
    <Files *.js>
    AddHandler application/x-httpd-php .js
    </Files>
    Code (markup):
     
    codyrockx, Oct 2, 2007 IP