Question

Essentially, I'm completely new at PHP - using dreamweaver cs6 (its all i have)

I have to get the value of a query string (someurl.com/id=something). After i get the value, I need to show a single div based on the ID.

While I know there really isn't any REAL "page load" event for PHP, I'd like to have it when the page opens.

So, how do I accomplish this?

I started with this:

<?php
    $stringId = $_GET['id']
   ?>

    <script language="javascript">      
        document.getElementById('$stringId').style.display = "block";
    </script>
    <?
?>

Since I don't have a server setup, will something like this work? Would you suggest something else? Am I completely wrong?

Any input would be appreciated. Thank you.

Was it helpful?

Solution

You should set the style inline with out relying on javascript as some users may have javascript turned off.

<div <?php if($_GET['id'] == 'X'){ ?>style="display: block;"<?php } ?>>
    your div content
</div>

with an else as requested

<div style="<?php if($_GET['id'] == 'X'){ ?>display: block;<?php } else { ?>display: none;<?php } ?">
    your div content
</div>

you could also use a predefined css class

based on your comment about having way to many to do a switch if you need to mix it up just insert this

<script language="javascript">      
    document.getElementById('<?php echo $_GET['id']; ?>').style.display = "block";
</script>

OTHER TIPS

It should be something like this:

<?php
switch($_GET['id']) {
    case 'asdf':
        echo '<div>ASDF in QS</div>';
        break;
    case 'bcd':
        echo '<div class="some-class-because-i-can">something else</div>';
}

You can't simply mix javascript with php.

And of course, you need servers setup, server built in php(http://php.net/manual/en/features.commandline.webserver.php) should be enough for your needs.

PHP is server-side scripting language, meaning it only runs on the server (any output being rendered on the users screen), that being said you need a local server like WAMP XAMP to debug/ run your code.

What you are trying to do is not going to work PHP is executed on the server as i previously mentioned and Javascript runs in the viewers browser, the variable $stringID will not be present when your javascript code runs.

Why bother hiding an element that has the url ID when PHP can render that element and only that element.

<?php
    if(isset($_GET['id'])) {   //checks weather the link has data in the id field
       $stringId = $_GET['id'];

       echo "<div> content here </div>"; //anything you echo will be rendered in the 
                                         //pages source code be it HTML, Javascript etc.
    }
>?

If you want to fetch this data from php without refreshing the page use AJAX from jQuery (google it :D)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top