Мой сайт работает на одной странице с динамическими изменениями 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 при нажатии кнопки?
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>
Когда вы передаете значение по URL-адресу, используйте $ _GET для доступа к нему
<link rel="stylesheet" type="text/css" href="css/<?php echo $_GET['css']; ?>.php" />
if(isset($_GET['css'])) {
echo '<link rel="stylesheet" type="text/css" href="css/'.$_GET['css'].'.php" />';
}
Лучше держите свой 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'; ?>" />
index.php?css=abcd
какой css будет загружен
onclick="index.php?css=2"
Это недопустимое значение атрибута. Я хочу манипулировать переменной внутри php.