SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing info from PHP to javascript?

    I need to pass some information from PHP to javascript in some fashion, and I could use a hand (or, more likely, several) with how to do this as I don't know much about either PHP or javascript.

    This is what I have so far for the PHP:

    Code:
    <?php 
    
    $MenuItem = Array ( 
    '/Citadel/' => None,
    '/Citadel/FAQ' => QA,
    '/Citadel/SSM' => QA,
    '/Citadel/Concordance' => Theme,
    '/Citadel/Encyclopaedia' => Theme,
    '/Citadel/Heraldry' => Theme,
    '/Citadel/History' => Theme,
    '/Citadel/Characters' => Story,
    '/Citadel/Prophecies' => Story,
    '/Citadel/Artwork' => Miscellany,
    '/Citadel/Books' => Miscellany,
    '/Citadel/Images' => Miscellany,
    '/Citadel/Links' => Miscellany
    ); 
    
    $SubMenu = Array ( 
    '/Citadel/' => None,
    '/Citadel/FAQ' => QA,
    '/Citadel/SSM' => QA,
    '/Citadel/Concordance' => Theme,
    '/Citadel/Encyclopaedia' => Theme,
    '/Citadel/Heraldry' => Theme,
    '/Citadel/History' => Theme,
    '/Citadel/Characters' => Story,
    '/Citadel/Prophecies' => Story,
    '/Citadel/Artwork' => Miscellany,
    '/Citadel/Books' => Miscellany,
    '/Citadel/Images' => Miscellany,
    '/Citadel/Links' => Miscellany
    ); 
    
    $SubMenuItem = Array ( 
    '/Citadel/' => None,
    '/Citadel/FAQ' => FAQ,
    '/Citadel/SSM' => SSM,
    '/Citadel/Concordance' => Concordance,
    '/Citadel/Encyclopaedia' => Encyclopaedia,
    '/Citadel/Heraldry' => Heraldry,
    '/Citadel/History' => History,
    '/Citadel/Characters' => Characters,
    '/Citadel/Prophecies' => Prophecies,
    '/Citadel/Artwork' => Artwork,
    '/Citadel/Books' => Books,
    '/Citadel/Images' => Images,
    '/Citadel/Links' => Links
    ); 
    
    $URL = $_SERVER['PHP_SELF'];
    
    $Folders = substr($URL,0,strrpos($URL,"/"));
    
    $WhichMenuItem = $MenuItem[$Folders];
    
    $WhichSubMenu = $SubMenu[$Folders];
    
    $WhichSubMenuItem = $SubMenuItem[$Folders];
    
    ?>
    I am not absolutely certain that the above works as intended (for example, sets $WhichMenuItem to 'QA' for pages in the /Citadel/FAQ folder), as I have had a lot of help with pieceing it together. The idea is that it is supposed to go on each page to determine in which folder that page resides. Will it work to just use a php include to put this code (its in a separate file) anywhere on each page? Or is there anywhere that php includes can't go, like before </head>?

    The javascript, then, is supposed to use the information about where the page is located to load up the right submenu.

    Page: http://www.westeros.org/Citadel/
    Javascript: http://www.westeros.org/Citadel/Mouseovers.js

    At the moment, the pure javascript works fine, but the part that is supposed to use the information from PHP to load up the right menu on interior pages isn't working, and I don't know enough to determine if the error lies with the PHP or the javascript.

  2. #2
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    Maybe this will help you.

    $phpArray is a php array and we create a javascript Array called javascriptArray
    We put the php array values into the javascript array.


    $phpArray = array('one','two','three','four','five','six');

    echo "<script language='javascript' >\n";
    echo "javascriptArray = new Array; >\n";

    for ( $i=0; $i < count( $phpArray); $i++) {
    echo "javascriptArray[".$i."] = " . $phpArray[$i] . ";\n";
    }
    echo "</script>\n";

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm afraid I don't quite understand how that is meant to work.

    Would I replace my current code with that, or is it supposed to complement it?

  4. #4
    SitePoint Enthusiast devonmallory's Avatar
    Join Date
    Apr 2005
    Location
    Toronto, Canada
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What dadidos is has shown is that you can write javascript code with php, just like you can write html. There isn't really any other way to get data from a php script to a javascript that I know of....

    The problem you may experience is that you want the external .js file to get the information, rather than having it as a <script> tag in your html page. Hard to say what you should do about this, but you may need to resort to the <script> tag that dadidos showed...
    "It's all in the reflexes" - Jack Burton

  5. #5
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another way would be to format your data approriately to pass to a javascript function. For a very general example, you could try:
    PHP Code:
    $mystring "value1;value2;value3";
    echo (
    "<input type='button' name='somebutton' value='click me' onclick='return yourjsfunc(\'" $mystring "\');' />"); 
    That creates a string in php and passes it to a js function in the onclick event of a button. This way you can write out the js function call and pass it PHP variables but have an external .js file which holds your javascript function.

  6. #6
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should look into either Sajax or JPSpan. Either library will let you build an Ajax client.
    Christopher

  7. #7
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    devonmallory,

    Hrm ... okay, I sort of see what you say. If I understand it correctly, could use the PHP to write out the right version of this piece of code for each folder:

    Code:
    oldWindowOnload = window.onload;
    window.onload = function() {
        if (oldWindowOnload)
            oldWindowOnload();
        ToggleSub(<value dependant on folder>);
    }
    Maybe something like this?

    Code:
    if ($SubMenu[$Folders]=="1") 
    
    echo "<script language='javascript' >\n";
    echo "oldWindowOnload = window.onload;\n";
    echo "window.onload = function() {\n";
    echo "if (oldWindowOnload)\n";
    echo "oldWindowOnload();\n";
    echo "ToggleSub(QA);\n";
    echo "</script>\n";
    Not ideal, since it means I don't get the saving of having it externally ... but I don't have much of a choice it seems if I plan to get this menu working. Its too bad it couldn't be done completely in either PHP or Javascript.

    Edited to add: Hrm ... well, that doesn't seem to work, at least, so I've done something wrong.

    krayziepjf,

    If I understand your explanation correctly, it won't work in this situation because the javascript has to trigger automatically as the page is loaded. I am trying to ensure that interior pages of a site will load with the right menu, to make it plain to the visitor which section they are in.

    arborint,

    I'm afraid that is way beyond me. I hardly know anything about javascript or PHP even -- I've had help with pretty much all the code, which is why getting it to work together is so difficult.
    Last edited by Linda A; Apr 28, 2005 at 09:03.

  8. #8
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    'm afraid that is way beyond me. I hardly know anything about javascript or PHP even -- I've had help with pretty much all the code, which is why getting it to work together is so difficult.
    That's why I recommended Sajax or JPSpan. They are pre-written libraries that can do this kind of communication for you. They come with examples I believe.
    Christopher

  9. #9
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, I see. Although, looking at the FAQs for those, they still seem to require a fair understanding of PHP and javascript to use. I'll take a closer look though if I can't find another solution (like doing it all in javascript).

  10. #10
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For loading the menus when the page displays, why not call your javascript function using my example on the onload event?

    html version:
    HTML Code:
    <body onload="displaymenu('some information');">
    php version:
    PHP Code:
    echo("<body onload='displaymenu(/'' . $yourvariable . "/');'>"); 

  11. #11
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought that using the onload even wasn't considered appropriate any longer? If that's not so, that's an option.

    Although, can you have more than one onload event? Apart from calling up the right menu, I need two more functions trigger.

  12. #12
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At this point, you might want to consider posting this in the javascript forum. I'm not sure if onload is a supported event trigger, or if a page can have multiple onload events.

    Sorry I couldn't be of more help with this.

  13. #13
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No prob, I appreciate the effort

    I'll try the javascript forum again -- problem has been trying to combine the advice on the PHP end with the advice on the javascript end.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •