上周第一次开始使用 PHP。我正在尝试根据表单中的用户输入来更改背景图像。这是 PHP 代码:
if ($gender == 'Male')
echo '<body style="background-color: red">';
if ($gender == 'Female')
echo '<body style="background-image: url("../images/female.jpg")">';
我可以根据选择更改背景颜色,但是当我尝试将其更改为图像时,没有任何反应。图像在正确的路径中。不确定是否可以通过这种方式完成。我知道可能还有其他方法可以做到这一点,但可能不适合我的 php 编码水平(只是初学者)。
感谢您的帮助。
不设置 CSS 类:
if ($gender == 'Male')
echo '<body style="background-color: red">';
if ($gender == 'Female')
echo '<body style="background-image: url(../images/female.jpg)">';
但是像@hungerstar 所说的那样定义 CSS 类会很好
你的 CSS 文件:
.male {
background-color: red
}
.female {
background-image: url(../images/female.jpg)
}
你的 php 代码和 css 类:
if ($gender == 'Male')
echo '<body class="male"';
if ($gender == 'Female')
echo '<body class="female">';