duanji1924 2017-04-30 04:38
浏览 51
已采纳

Checkbox数组的未定义索引

I am sending an array of checkbox values from a Form to my controller.
I get no value when I try to access them using $_Post. So I decided to try using var_dump(). The result is that I see no value inside my array. If i check 3 checkboxes I get:

array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" }

I don't understand how to correct this error.

-- My form --

<div class="container">
<div>
    <form class="form-horizontal" id="form" action="/MVC/home/languageChosen" method="post">
        <div class="form-group">
            <table>
                <tr>
                    <th>Language</th>
                    <th>Learn</th>
                    <th>Teach</th>
                </tr>
                <?php foreach ($data['languageData'] as $languageData): ?>
                    <tr>
                        <td><?php echo $languageData['language_name'] ?></td>
                        <td><input type="checkbox" name="learn[]" value="<?php $languageData['language_id'] ?>"></td>
                        <td><input type="checkbox" name="teach[]" value="<?php $languageData['language_id'] ?>"></td>
                    </tr>
                <?php endforeach ?>
            </table>
            <button id="continueButton" name="action" type="submit" class="btn btn-default" value="RegisterLanguage">Continue</button>
        </div>
    </form>
</div>

-- Controller --

public function languageChosen()
{
    echo "You posted: " . implode(',', $_POST['learn']); // This results in: You posted: ,,
    var_dump($_POST['learn']);
    die();
}

EDIT

When I echo $languageData['language_id'], it results in the image below.

enter image description here

  • 写回答

1条回答 默认 最新

  • dtjwov4984 2017-04-30 05:34
    关注

    You are not getting values because you didn't echo it out.

    Check here

                    <tr>
                        <td><?php echo $languageData['language_name'] ?></td>
                        <td><input type="checkbox" name="learn[]" value="<?php $languageData['language_id'] ?>"></td>
                        <td><input type="checkbox" name="teach[]" value="<?php $languageData['language_id'] ?>"></td>
                    </tr>
    

    Change the above code as like this by adding echo on $languageData

                    <tr>
                        <td><?php echo $languageData['language_name'] ?></td>
                        <td><input type="checkbox" name="learn[]" value="<?php echo $languageData['language_id'] ?>"></td>
                        <td><input type="checkbox" name="teach[]" value="<?php echo $languageData['language_id'] ?>"></td>
                    </tr>
    

    and i hope this solve your problem.. Cheers !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等