下拉声明后PHP变量不起作用

时间:2014-05-24 10:31:43

标签: php variables

我一直试图找出为什么我的php变量在声明下拉列表后没有工作。在通知声明的所有行上回显变量之后,我将其缩小到下拉列表,我发现变量在echo "<select id ='fav' name='fav'>";为什么会发生后直接停止回声?以及如何解决?

<?php
$fruit= $_POST['fruit'];
echo $fruit;
echo "<table>";
echo $fruit;
echo "<tr>Favourite Fruit:";
echo $fruit;
echo "<select id ='fav' name='fav'>";
echo $fruit; //this doesn't echo
echo "<option value='Banana'>Banana</option>";
echo "<option value='Strawberry'>Strawberry</option>";
echo "<option value='No Preference'>No Preference</option>";
echo "</select>";
?>

5 个答案:

答案 0 :(得分:1)

您无法直接打印任何值。试试这个适合你。

<?php
                        $fruit= $_POST['fruit'];
                        echo $fruit;
                        echo "<table>";
                        echo $fruit;
                        echo "<tr>Favourite Fruit:";
                        echo $fruit;
                        echo "<select id ='fav' name='fav'>";
                        echo "<option value='".$fruit."'>".$fruit."</option>";
                        .........................
                        .........................
                        echo "<option value='No Preference'>No Preference</option>";
                        </select>
                        ?>

答案 1 :(得分:1)

如果您查看使用浏览器生成的HTML&#34;查看源代码&#34;选项,您可能会看到回声工作正常。您无法看到它的原因是您尝试将文本放在<select>内,而不是放在任何<option>内,因此屏幕上无法显示。这与PHP变量无关。

答案 2 :(得分:0)

因为您忘了关闭select tag.close select tag:

            <?php
            $fruit= $_POST['fruit'];
            echo $fruit;
            echo "<table>";
            echo $fruit;
            echo "<tr>Favourite Fruit:";
            echo $fruit;
            echo "<select id ='fav' name='fav'></select>";
            echo $fruit; 
            ?>

答案 3 :(得分:0)

我想你想要这个,如果你把东西放进去

<select></select>

你必须使用

<option></option>

或者你可以这样做,

  echo $fruit; //this doesn't echo
  echo "<select id ='fav' name='fav'>";

但不喜欢,

 echo "<select id ='fav' name='fav'>";
 echo $fruit; //this doesn't echo

完整代码将是

<?php
$fruit= $_POST['fruit'];
echo $fruit;
echo "<table>";
echo $fruit;
echo "<tr>Favourite Fruit:";
echo $fruit;
echo "<select id ='fav' name='fav'>";
echo '<option value="';
echo $fruit . '">';
echo $fruit;
echo '</option>';
echo '</select>';
.........................
.........................
.........................
?>

答案 4 :(得分:0)

您的代码应该是这样的,如果没有任何$fruit代码,您就无法致电option

$fruit= $_POST['fruit'];
echo "<select id ='fav' name='fav'>";
echo '<option value="';
echo $fruit . '">';
echo $fruit;
echo '</option>';
echo '</select>';
.........................