PHP - 动态更改变量

时间:2014-02-18 06:11:21

标签: php html css

我的网站在一个包含动态CSS更改的网页上运行。如果我想将客户端指向页面内容,我所做的就是给页面链接传递CSS id。即: www.ilstours.net/index.php?css=1

的index.php:

    <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="css/<?php echo $css; ?>.php" />
    </head>
    <body>
<button id="button1" type="button" onclick="index.php?css=2">Update</button> 'HERE
    </body>
    </html>

1.PHP:

<?php
header("Content-type: text/css; charset: UTF-8");
$Color = "black";
?>
body {background-color:<?php echo $Color ?>;}

2.PHP:

  <?php
    header("Content-type: text/css; charset: UTF-8");
    $Color = "red";
    ?>
    body {background-color:<?php echo $Color ?>;}

如何在点击按钮时切换到CSS文件2.php?

4 个答案:

答案 0 :(得分:0)

当你通过url传递值时,使用$ _GET来访问它

<link rel="stylesheet" type="text/css" href="css/<?php echo $_GET['css']; ?>.php" />

答案 1 :(得分:0)

<强>的index.php

<?php
$css = 1; // default css (1)
if(isset($_REQUEST['css']) && ($_REQUEST['css']==1 || $_REQUEST['css']==2))
{
  $css = $_REQUEST['css']; // update by click on button (1 or 2)
}

?>
      <!DOCTYPE html>
        <html>
        <head>
        <link rel="stylesheet" type="text/css" href="css/<?php echo $css; ?>.php" />
        </head>
        <body>
            <button id="button1" type="button" onclick="location.href='index.php?css=1';">Update CSS 1</button>
            <button id="button1" type="button" onclick="location.href='index.php?css=2';">Update CSS 2</button>

        </body>
        </html>

答案 2 :(得分:0)

if(isset($_GET['css'])) {
    echo '<link rel="stylesheet" type="text/css" href="css/'.$_GET['css'].'.php" />';

}

答案 3 :(得分:-1)

最好将你的css保持在单个数组中,如

<?php
   $my_css = array("1"=>"1.css","2"=>"2.css",'deflt' => 'defualt.css');
?>

并包含css,如

<link rel="stylesheet" type="text/css" href="css/<?php echo (isset($_GET['css']) && isset($my_css[$_GET['css']])) ? $my_css[$_GET['css']] : 'defualt.css'; ?>" />