我对编程非常陌生。我试图从选择框中获取变量,两个文本输入框正常工作。当我提交表格时,即使选择了其他选项,红猫也会一直出现。
<form name="cat" action="" method="post">
Name of Cat: <input class="inputbox" type="text" name="name" value="" /><br />
Color: <select name="color">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="black">Black</option>
</select> <br>
Weight: <input class="inputbox" type="number" name="weight" value="" /><br />
<input type="submit" class="submit" name="submit" value="Submit" />
</form>
<br>
<br>
<?php
if ($_POST['submit']) {
$name = $_POST['name'];
$color = $_POST['color'];
$weight = $_POST['weight'];
if ($color = 'red') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/9/7/D/t/L/K/red-cat-md.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color = 'blue') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/L/z/H/T/d/d/blue-cat-md.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color = 'yellow') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/O/Y/K/K/u/r/yellow-cat-hi.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color = 'black') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://content.mycutegraphics.com/graphics/halloween/cute-halloween-black-cat.png'></a><br>" . $weight . " (kg)<br>";
} else {
//do nothing
}
}
答案 0 :(得分:1)
您的情况有误:if($ color ='red'),请尝试下面的代码
if ($_POST['submit']) {
$name = $_POST['name'];
$color = $_POST['color'];
$weight = $_POST['weight'];
if ($color == 'red') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/9/7/D/t/L/K/red-cat-md.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color == 'blue') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/L/z/H/T/d/d/blue-cat-md.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color == 'yellow') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://www.clker.com/cliparts/O/Y/K/K/u/r/yellow-cat-hi.png'></a><br>" . $weight . " (kg)<br>";
} else if ($color == 'black') {
echo "<h2>" . $name . "</h2>";
echo "<img src='http://content.mycutegraphics.com/graphics/halloween/cute-halloween-black-cat.png'></a><br>" . $weight . " (kg)<br>";
} else {
//do nothing
}
答案 1 :(得分:1)
您正在使用单个等号来分配变量:
$variable = 'foo';
执行条件语句时需要双等号:
if ($variable == 'foo') {
答案 2 :(得分:0)
请尝试将此代码添加到您的代码中,以便在选择了某个选项并提交后,该页面将显示您在加载时选择的内容
<select name="color" value="<?php echo $_POST['color']; ?>">