HTML表单复选框无法在PHP中输出值

时间:2018-02-21 05:52:52

标签: php html

我对HTML和PHP语言比较陌生。我正在制作调查表并使用复选框允许用户选择他们认为在大学期间最有用的所有课程。

在我的HTML代码中,我有以下表单元素:

<table>
    <tr>
        <td>
            <input type="checkbox" name="useful_classes[]" value="Engineering Economics">
            <td align="left">Engineering Economics
            </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" name="useful_classes[]" value="Operations Research">
            <td align="left">Operations Research
            </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" name="useful_classes[]" value="Simulation">
            <td align="left">Simulation
            </td>
    </tr>
</table>

下面是我的PHP代码,我试图用它来为他们制作提交页面,以验证他们刚提交的所有内容是否正确。我无法弄清楚如何让PHP代码输出类似于&#34;工程经济学,运筹学等的东西。或类似的东西。无论我尝试从谷歌上发现的其他东西,它都不起作用。

<?PHP
            $useful_classes = $_POST['useful_classes'];

            print <<<END_PRINT
            <table>

                <tr>
                    <td class="title" align="right" > <b>Most Useful Classes:</b> </td>
                    <td class="values" align="left" > 
                        foreach ($useful_classes as $useful_classes){ 
        echo $useful_classes . ", ";
    }
                            /* for($i=0; $i<count($useful_classes); $i++){
                                echo($useful_classes[$i]);
    if($i !=$N){
                                    echo(", ");
                                }
                            } */
                    </td>
                </tr>
            </table>
    END_PRINT;
    ?>

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

<?PHP
    if(isset($_POST['submit']))
    {
        $useful_classes = $_POST['useful_classes'];
?>
        <table>
            <tr>
                <td class="title" align="right" > <b>Most Useful Classes:</b> </td>
                <td class="values" align="left" > 
                <?php
                    foreach ($useful_classes as $useful_classes){ 
                        echo $useful_classes . ",";
                    }
                ?>

                </td>
            </tr>
        </table>
<?php 
    }
?>

<form action="" method="post">
<table>
    <tr>
        <td>
            <input type="checkbox" name="useful_classes[]" value="Engineering Economics">
            <td align="left">Engineering Economics
            </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" name="useful_classes[]" value="Operations Research">
            <td align="left">Operations Research
            </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" name="useful_classes[]" value="Simulation">
            <td align="left">Simulation
            </td>
    </tr>
    <input type="submit" name="submit">
</table>
</form>

而不是你可以通过Implode Function

轻松完成
if(isset($_POST['submit']))
    {
        echo   $useful_classes = implode(",",$_POST['useful_classes']);

    }