无法从PHP中获取所选选项

时间:2014-05-19 03:40:32

标签: php

我想从下面的php代码中选择一个选项。我一直面临一个错误,我不知道为什么我无法获得价值。

<html>
    <head>
    </head>
    <body>
        <label>Please select a make below</label> <p></p>
    </body>
</html>

<?php
    include("cars.php");

    if (!class_exists("Cars"))
        echo"<p>The Cars class is not available!</p>";
    else
    {
        $cars = new Cars();

        getList($cars);

            echo $_POST["makesList"]; // I thought I could get the selected with this code but it displays the following error "Undefined index: makesList" 

    }

    function getList($cars)
    {
        $makes = $cars->getMakes();

        $makes=array_unique($makes);

        echo '<form name="makesForm" id="makesForm" method="POST" action="showInventory.php">';
        echo '<select name="makesList" id="makesList">';

        foreach($makes as $make)
            echo '<option value='.$make.'>'.$make.'</option>';
        echo '</select>';
        echo '</form>';



    }
?>

是否有其他方法可以获得所选值?仅供参考,我在Cars.php上课。我认为你不需要关心这一点。它通过getList()函数显示列表。

1 个答案:

答案 0 :(得分:0)

请尝试使用此代码,并建议是否有效。

<?php
    include("cars.php");
    if (!class_exists("Cars"))
        echo"<p>The Cars class is not available!</p>";
    else{
            $cars = new Cars();
            getList($cars);
                echo $_POST["makesList"]; // I thought I could get the selected with this code but it displays the following error "Undefined index: makesList"
            }

    function getList($cars){
        $makes = $cars->getMakes();
        $makes=array_unique($makes);
        echo '<form name="makesForm" id="makesForm" method="POST" action="showInventory.php">';
        echo '<select name="makesList" id="makesList">';
        foreach($makes as $make)
            echo '<option value='.$make.'>'.$make.'</option>';
            echo '</select>';
            echo '</form>';
    }
?>