简体   繁体   中英

Add margin-left, script inside php

The problem is. I want to inset margin-left: 20%; to maincontent, when the user is logged in.

No i dont have a problem with syntax, i can get it done in js, but im told to use php. So i also added a class in css and to my body.

<?php
if ($_SESSION['username']){


include("/view/leftmenu.php");

}
?>

How do i activate css in php?

As revealed by the syntax highlighting, you're trying to put a single quote inside a single-quoted string.

Your options are:

  1. Escape the single quotes with \\' .
  2. Use double quotes instead.
  3. Use ?> ... <?php instead of echo .
  4. Don't do this with JavaScript at all! You control the server side; why not just add a class to the body like logged-in , and have a CSS rule like body.logged-in #maincontent { margin-left: 20%; } body.logged-in #maincontent { margin-left: 20%; } ?

(For what it's worth your JavaScript is invalid too; you need to quote the 20% . Percentages aren't legal JS.)

Put your JavaScript in double quotes, with single quotes inside. You may want to run your include first as well. include does not need () . Your code could look more like:

<?php
session_start(); // has to be run before any headers are sent
if(isset($_SESSION['username'])){
  include '/view/leftmenu.php';
  echo "<script type='text/javascript'>$('#maincontent').css('margin-left', '20%');</script>";
}
?>

The better solution however would look more like:

<?php
session_start(); $marginLeft = '0'; // $marginLeft should be your default
if(isset($_SESSION['username'])){
  $marginLeft = '20%';
  include '/view/leftmenu.php';
}
echo "<!DOCTYPE html><html><head><style type='text/css'>".
"#maincontent{margin-left:$marginLeft;}</style></head><body>".
"<div id='maincontent'>content</div></body></html>";
?>

A better approach yet, would look like:

<?php
session_start(); $className = 'withoutMargin'; // $marginLeft should be your default
if(isset($_SESSION['username'])){
  $className = 'withMargin';
  include '/view/leftmenu.php';
}
?>
<!DOCTYPE html>
<html>
  <head>
    <style type='text/css'>
      .withoutMargin{
         margin-left:0;
       }
       .withMargin{
         margin-left:20%;
       }
    </style>
  </head>
<body>
<?php echo "  <div class='$className'>"; ?>
  <!-- your content here -->
  </div>
</body>
</html>

Note: Your code above does not have to look exactly like this. This just illustrates concept. You would have more code testing for submit to be set and so forth. Also, with the second example, which I recommend, I would use external CSS, so it is cached by the user's Browser.

20% needs to be in quotes:

$('#maincontent').css('margin-left', '20%');

This also applies to px, em, pt, and any other form of CSS unit of measurement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM