PHP 中的 CSS 回声
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7462043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS in PHP echo
提问by connor.p
I am having a problem with the following piece of code:
我在使用以下代码时遇到问题:
$submit = $_POST['submit'];
$answer = "8";
$input = strip_tags($_POST['input']);
if ($submit){
if ($input==$answer){
echo "Correct";
}
else
echo "Wrong";
CSS:
CSS:
.wrong { ? ? ?
margin-top: 5px; ? ? ??
padding: 5px; ? ?
background-color:#F00; ??
border: 2px solid #666; ? ?
width:auto;
color: #000000; ??
}
All I want is to put a little bit of CSS in with an PHP echo command. If the user gets the answer wrong a red box should appear with "Wrong" in the middle.
我想要的只是在 PHP echo 命令中加入一点 CSS。如果用户回答错误,则会出现一个红色框,中间带有“错误”。
I have already tried
我已经试过了
echo <div class="wrong">"Wrong"</div>;
but that did not work.
但这没有用。
回答by Lekensteyn
PHP interprets the quote character special, it marks the start or end of a string literal. Either escape your quotes using a backslash or use other single quotes:
PHP 解释引号字符特殊,它标记字符串文字的开始或结束。要么使用反斜杠转义引号,要么使用其他单引号:
<style>
.wrong {
margin-top: 5px;
padding: 5px;
background-color: #F00;
border: 2px solid #666;
width: auto;
color: #000000;
}
</style>
<?php
$submit = $_POST['submit'];
$answer = "8";
$input = strip_tags($_POST['input']);
if ($submit) {
if ($input == $answer) {
echo "Correct";
} else {
// note: escaped the quote character using a backslash
echo "<div class=\"wrong\">wrong</div>";
// alternative:
//echo '<div class="wrong">wrong</div>';
}
}
?>
See also the PHP manual on the string type.
回答by Astha
please try as:
请尝试:
echo '<div class="wrong">Wrong</div>';
missing quotes for echo statement.
缺少 echo 语句的引号。