1

Im trying to do a link from one page to another, on the link itself I send a variable that the receiving page gets with php, then I need the php to echo out js to change the css display on two divs, changing the div whose default css is block to that of hidden and a second div whose default css is none to that of block. I have suucesfully passed the variable within the url, and the js to change the second div to block works, however the 1st div css doesnt seem to change to none as the div still shows

PHP code

if (isset($_GET['display'])) {

    if(!isset($_GET['yes'])) {

        echo '<style type="text/css">

              #slideshow {
                  display: none;
              }
              </style>';

        echo '<style type="text/css">

              #aboutus {
                  display: block;
              }
              </style>';
    }

}else{



};

Can anyone help me resolve this please

6
  • 4
    Seems like a horrible idea, just use a class on the element or something like that instead ? Commented May 1, 2015 at 22:01
  • 2
    Sounds like the problem is with the Javascript, not the PHP. Show that. Commented May 1, 2015 at 22:07
  • You can remove else PHP part since it is empty --- else{ }; The above code is inside header? If inline CSS is acceptable, you may skip the javascript part altogether and apply CSS just with PHP. Commented May 1, 2015 at 22:14
  • 1
    How does your url query looks like? If it's ?display=yes , then instead of if(!isset($_GET['yes'])) use if(trim($_GET['display']) != 'yes') Commented May 1, 2015 at 22:20
  • @adeneo, I agree. I would change the class of the divs as I emit/echo them and put the css where it belongs in the css file. Commented May 1, 2015 at 22:32

2 Answers 2

1

Are you sure you don't mean this:

if($_GET['display'] == 'yes') {

I would pass an integer value rather than 'yes' and 'no'.

Then do it this way:

<?php
ob_start("ob_gzhandler");
header('Content-Type: text/html; charset=utf-8');
header('Connection: Keep-Alive');
header('Keep-Alive: timeout=5, max=100');
header('Cache-Control: max-age=84600');
header('Vary: Accept-Encoding');
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>page Title</title>
<style type="text/css">
.hide{display:none;}
</style>
</head><body><div id="page">

EOT;
ob_flush();

$display = inval($_GET['display']); 

$aboutclass = array(' class="hide" ','');
$slideclass = array('',' class="hide" ');

echo '<div id="aboutus" ' . $aboutclass [$display] . '>'
echo '<div id="slideshow" ' . $slideclass [$display] . '>'


echo '</div></body></html>';
ob_end_flush();
Sign up to request clarification or add additional context in comments.

1 Comment

Do I put the css styling in the header? thats where I put it, then the rest in the php, after resolving some syntax errors I saved and loaded the page and got a blank page, perhaps I havent iserted this correctly
0

I found a solution, using the following code

PHP

if(isset($_GET['display']) != 'yes'){

$slider = 'block';


$aboutus = 'none';


}else{

$slider = 'none';

$aboutus = 'block';

}

Then I removed the display stying from the external stylesheet for the .slideShow and .aboutus and added inline styles just for the display within the head section of the html code as follows

<style type="text/css">

.sliderWrapper {
    display: <?php echo $slider; ?>
}

.aboutUs {
    display: <?php echo $aboutus; ?>

    };
</style>

Then it worked as required.

3 Comments

You can further simplify and optimize the above code and write it to be much faster and keep external css part for performance and clarity (your original code was on a right path), but it didn't work because of wrong second if line in PHP. As already told no need for javascript at all.
I only added the second if to try and get the code working, as I said I got the about us div to show but couldnt get the slideshow div to hide, and I agree logically it should have worked
Also I need to do this four or five times from other pages so I know my skills arent evolved enough to find a simpler way to do this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.