我正在尝试动态设置CSS属性。但由于某些原因,使用post方法进行此操作不起作用。
这有效:
<?php header("Content-type: text/css",
"Location: http://localhost/template/index.php");
$backgcol = '#333';
?>
body
{
background-color:<?=$backgcol?>;
}
但这不起作用:
<?php header("Content-type: text/css", "Location: http://localhost/template/index.php");
$backgcol = $_POST['color'];
?>
body
{
background-color:<?=$backgcol?>;
}
这是html:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.php">
</head>
<body>
<form action="style.php" method="post">
<input type="text" name="color"><br>
<input type="submit" value="Set color">
</form>
</body>
</html>
答案 0 :(得分:0)
你对太多的观点有误解。
<?php
header('Content-Type: text/css');
header('Location: http://localhost/template/index.php');
Location
标头与CSS文件请求无关。$_POST
值不再设置在CSS文件请求中。$_POST
变量在每个请求上都是临时的。它无法保存。有效示例:
<强> setcolor.php 强>
<?php
const COLOR_REGEX = '/\A#(?:(?:[\da-f]{3}){1,2}+|AliceBlue|AntiqueWhite|Aqua|Aquamarine|Azure|Beige|Bisque|Black|BlanchedAlmond|Blue|BlueViolet|Brown|BurlyWood|CadetBlue|Chartreuse|Chocolate|Coral|CornflowerBlue|Cornsilk|Crimson|Cyan|DarkBlue|DarkCyan|DarkGoldenRod|DarkGray|DarkGreen|DarkKhaki|DarkMagenta|DarkOliveGreen|DarkOrange|DarkOrchid|DarkRed|DarkSalmon|DarkSeaGreen|DarkSlateBlue|DarkSlateGray|DarkTurquoise|DarkViolet|DeepPink|DeepSkyBlue|DimGray|DodgerBlue|FireBrick|FloralWhite|ForestGreen|Fuchsia|Gainsboro|GhostWhite|Gold|GoldenRod|Gray|Green|GreenYellow|HoneyDew|HotPink|IndianRed|Indigo|Ivory|Khaki|Lavender|LavenderBlush|LawnGreen|LemonChiffon|LightBlue|LightCoral|LightCyan|LightGoldenRodYellow|LightGray|LightGreen|LightPink|LightSalmon|LightSeaGreen|LightSkyBlue|LightSlateGray|LightSteelBlue|LightYellow|Lime|LimeGreen|Linen|Magenta|Maroon|MediumAquaMarine|MediumBlue|MediumOrchid|MediumPurple|MediumSeaGreen|MediumSlateBlue|MediumSpringGreen|MediumTurquoise|MediumVioletRed|MidnightBlue|MintCream|MistyRose|Moccasin|NavajoWhite|Navy|OldLace|Olive|OliveDrab|Orange|OrangeRed|Orchid|PaleGoldenRod|PaleGreen|PaleTurquoise|PaleVioletRed|PapayaWhip|PeachPuff|Peru|Pink|Plum|PowderBlue|Purple|RebeccaPurple|Red|RosyBrown|RoyalBlue|SaddleBrown|Salmon|SandyBrown|SeaGreen|SeaShell|Sienna|Silver|SkyBlue|SlateBlue|SlateGray|Snow|SpringGreen|SteelBlue|Tan|Teal|Thistle|Tomato|Turquoise|Violet|Wheat|White|WhiteSmoke|Yellow|YellowGreen)\z/i';
function h($str) {
return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
}
$color = (string)filter_input(INPUT_POST, 'color');
if ($color !== null) {
if (preg_match(COLOR_REGEX, $color)) {
file_put_contents('color.dat', $color, LOCK_EX);
$msg = 'Color setting has been saved: ' . $color;
} else {
$msg = 'Color format is invalid:' . $color;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.php">
</head>
<body>
<?php if (isset($msg)): ?>
<p><?=h($msg)?></p>
<?php endif; ?>
<form action="" method="post">
<input type="text" name="color"><br>
<input type="submit" value="Set color">
</form>
</body>
</html>
<强> style.php 强>
<?php
header('Content-type: text/css');
$color = is_readable('color.dat') ? file_get_contents('color.dat') : "#ffffff";
?>
body {
background-color: <?=$color?>;
}