Use of PHP in Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Berkowitz
    New Member
    • Feb 2008
    • 5

    Use of PHP in Javascript

    Hi,

    I know it's not smart to use php and javascript together, but i need it. So I would like to know if this is possible.

    This is my php code, I'm reading out my database.


    [PHP]
    <?php foreach ($listRows as $record): ?>
    <?php $link1 = $record['link1'];
    $link2 = $record['link2'] ;
    $link3 = $record['link3'] ;
    $link4 = $record['link5'] ;
    $link5 = $record['link6'] ;
    $link6 = $record['link7']; ?>

    <?php endforeach ?>
    [/PHP]

    Now I need my $link vars in a javascript.
    Is This possible??

    This is my javascript.


    [CODE=javascript]
    <script language="javas cript">
    <!--

    stBM(2,"tree1d1 a",[0,"","","blank. gif",0,"left"," default","hand" ,0,0,-1,-1,-1,"none", 0,"#000000","tr ansparent",""," repeat",1,"defB utton_f%20.gif" ,"defButton_uf. gif", 9,9,1,"line_def 0.gif","line_de f1.gif","line_d ef2.gif","line_ def3.gif",1,0,3 ,1,"center",0, 0]);
    stBS("p0",[0,0]);
    stIT("p0i0",["Snel naar","","_self ","","","defIco n_f.gif","defIc on_uf.gif",18,1 8,"9pt 'Arial'","#0000 00","none","tra nsparent","","n o-repeat","9pt 'Arial'","#0000 00","underline" ,"transparent", "","no-repeat","9pt 'Arial'","#FFFF FF","none","#00 0099","","no-repeat","9pt 'Arial'","#FFFF FF","underline" ,"#000099",""," no-repeat",0,0,"le ft","middle",0, 0]);
    stBS("p1",[,1],"p0");
    stIT("p1i0",[$link1],"p0i0");
    stBS("p2",[],"p0");
    stIT("p2i0",["Netwerken",,,, ,"defIcon_c.gif ","defIcon_c.gi f"],"p0i0");
    stIT("p2i1",["Servers"],"p2i0");
    stES();
    stIT("p1i1",[$link2],"p2i0");
    stIT("p1i2",[$link3],"p2i0");
    stIT("p1i3",[$link4],"p2i0");
    stIT("p1i4",[$link5],"p2i0");
    stIT("p1i5",[$link6],"p2i0");
    stES();
    stES();
    stEM();
    //-->
    </script>
    [/CODE]

    I use it to make a tree structure (like windows) but I want the topics to be the one's from my database.

    $link obviously has to be replaced by the javascript var :)




    Help would be very much appriciated
    Last edited by acoder; Feb 22 '08, 03:41 PM.
  • Danigan
    New Member
    • Sep 2007
    • 18

    #2
    That's quite possible, but not in the way you're doing it. Keep in mind that PHP is all done with whatever it does by the time the code is sent to a web user. A user's machine can't see a PHP variable. JavaScript, however, is all sent to the user and all interpreted by the browser. Let me know if you have any questions on that part. Now for implementation. ..

    Let's say you only have one variable to do. Let me know if you need more than this:

    Code:
    <?php $link2 = $record['link2'] ; ?>
    
    <script language="javascript">
    stIT("p1i1",<?php echo '"' . $link2; . '"' ?>,"p2i0");
    </script>
    Now you have a whole insert of php code that echos the contents of the variable in $link2. When the webpage is sent to the user, the link is already a number, string, or whatever you had in it. Since it's the actual data from the variable on the user end (not a variable), you'll want quotes around it. I added that too. In php you concatenate with . if I remember right... sometime I confuse syntax like that since I program in a single language for a month or so and then switch. Hopefully this has helped, though. Let me know. :-)

    Comment

    • Berkowitz
      New Member
      • Feb 2008
      • 5

      #3
      Originally posted by Danigan
      That's quite possible, but not in the way you're doing it. Keep in mind that PHP is all done with whatever it does by the time the code is sent to a web user. A user's machine can't see a PHP variable. JavaScript, however, is all sent to the user and all interpreted by the browser. Let me know if you have any questions on that part. Now for implementation. ..

      Let's say you only have one variable to do. Let me know if you need more than this:

      Code:
      <?php $link2 = $record['link2'] ; ?>
      
      <script language="javascript">
      stIT("p1i1",<?php echo '"' . $link2; . '"' ?>,"p2i0");
      </script>
      Now you have a whole insert of php code that echos the contents of the variable in $link2. When the webpage is sent to the user, the link is already a number, string, or whatever you had in it. Since it's the actual data from the variable on the user end (not a variable), you'll want quotes around it. I added that too. In php you concatenate with . if I remember right... sometime I confuse syntax like that since I program in a single language for a month or so and then switch. Hopefully this has helped, though. Let me know. :-)


      Thanks for your reply and your trouble.
      I've tried it and now i get this


      Code:
      Parse error: syntax error, unexpected '.' in C:\hshome\oneideas\1id.be\testmenu.php on line 174

      Comment

      • Danigan
        New Member
        • Sep 2007
        • 18

        #4
        I can't give more detailed help till this evening. What's the code on that line? (And the line before and after.) I'll look again in 8 to 10 hours.

        Comment

        • Berkowitz
          New Member
          • Feb 2008
          • 5

          #5
          Originally posted by Danigan
          I can't give more detailed help till this evening. What's the code on that line? (And the line before and after.) I'll look again in 8 to 10 hours.

          [CODE=javascript]
          stBS("p1",[,1],"p0");
          stIT("p1i0",<?p hp echo '"' . $link1; . '"' ?>,"p0i0");
          stBS("p2",[],"p0");
          [/CODE]

          Comment

          Working...