www.webdeveloper.com
Results 1 to 7 of 7

Thread: passing variables into javascript.

  1. #1
    Join Date
    Dec 2005
    Posts
    62

    passing variables into javascript.

    I read another thread that seems to indicate that what I want to do can be done in php. I am quite new to php.

    my page is at http://www.stevelim.com/links/links.html

    what I am trying to do is pass parameters from other pages which will determine which CSS id to hide and show.

    here is the code that I think I should be using..

    Code:
    <?php if($_GET["type"] == "friends"){
    ?> 
    	<div id="friends" style="display: ''">
    	<?php
    	} else {
    	<div id="friends" style="display: 'none'">
    	?>
    
    	<?php
    	}
    	?>
    I get a parse error.

    Thanks.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    PHP Code:
    <?php if($_GET["type"] == "friends"){
    ?> 
        <div id="friends" style="display: ''">
        <?php
        
    } else {
        
    ?>
        <div id="friends" style="display: 'none'">

        <?php
        
    }
        
    ?>
    Personally, I find it easier to follow the code by not going in and out of PHP mode all the time, but that's more a matter of taste. Here's a cleaner way to do it:
    PHP Code:
    <?php
    $style 
    = ($_GET["type"] == "friends") ? '' 'style="display: none"';
    echo 
    "<div id=\"friends\"$style>\n";
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2005
    Posts
    62
    NogDog! Thank you! It works wonderfully... amazing this php. I'm often confused where javascript is needed and where php plays its role. I guess there is quite alot of overlap.

    As an aside. If I were to get greedy, may I ask if it is possible to sort and rearrange the html such that the variable type passed to the page gets listed first?

    Thanks again!

  4. #4
    Join Date
    Dec 2005
    Posts
    62
    Argh. sorry, I just noticed one more thing.

    Currently its a if / else thing. What if no type is sent at all? i would like all categories to be open as in my old page.

    Should I simple make a 3rd else statement?

  5. #5
    Join Date
    Dec 2005
    Posts
    62
    I think I just need the syntax for the following pseudocode..

    $style = ($_GET["type"] == "friends" OR _no type exists) ? '' : 'style="display: none"';

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    Quote Originally Posted by supersteve3d
    I think I just need the syntax for the following pseudocode..

    $style = ($_GET["type"] == "friends" OR _no type exists) ? '' : 'style="display: none"';
    PHP Code:
    $style = (!isset($_GET["type"]) or $_GET["type"] == "friends") ? '' 'style="display: none"'
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Dec 2005
    Posts
    62
    I got this to work using the following code.

    I guess I will address the sorting another time.

    Code:
    <?php
    $style = ($_GET["type"] == "friends" || $_GET["type"] == "") ? '' : 'style="display: none"';
    echo "<div id=\"friends\"$style>\n";
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles