【问题标题】:Radio buttons checked however passing NULL to database检查单选按钮,但将 NULL 传递给数据库
【发布时间】:2016-10-12 01:43:23
【问题描述】:

我有 4 组未选中的单选按钮呈现给用户。然后,用户需要为每个组检查一个单选按钮。当我提交这个表单时,我得到的错误是,例如,对于picture_6.tif,传递的值是NULL

我正在使用 CodeIgniter,并且表单正在通过 POST 提交。 我想知道你能不能告诉我我做错了什么。

<form action="/log" method="post" accept-charset="utf-8">
    <table class="static">
        <tbody>
            <tr>
                <td><input type="radio" name="picture_6.tif" value="0" class=""></td>
                <td><input type="radio" name="picture_7.tif" value="0" class=""></td>
                <td><input type="radio" name="picture_8.tif" value="0" class=""></td>
                <td><input type="radio" name="picture_9.tif" value="0" class=""></td>
            </tr>
            <tr>
                <td><input type="radio" name="picture_6.tif" value="1" class=""></td>
                <td><input type="radio" name="picture_7.tif" value="1" class=""></td>
                <td><input type="radio" name="picture_8.tif" value="1" class=""></td>
                <td><input type="radio" name="picture_9.tif" value="1" class=""></td>
            </tr>
            <tr>
                <td><input type="radio" name="picture_6.tif" value="2" class=""></td>
                <td><input type="radio" name="picture_7.tif" value="2" class=""></td>
                <td><input type="radio" name="picture_8.tif" value="2" class=""></td>
                <td><input type="radio" name="picture_9.tif" value="2" class=""></td>
            </tr>
            <tr>
                <td><input type="radio" name="picture_6.tif" value="3" class=""></td>
                <td><input type="radio" name="picture_7.tif" value="3" class=""></td>
                <td><input type="radio" name="picture_8.tif" value="3" class=""></td>
                <td><input type="radio" name="picture_9.tif" value="3" class=""></td>
            </tr>
            <tr>
                <td><input type="radio" name="picture_6.tif" value="9" class=""></td>
                <td><input type="radio" name="picture_7.tif" value="9" class=""></td>
                <td><input type="radio" name="picture_8.tif" value="9" class=""></td>
                <td><input type="radio" name="picture_9.tif" value="9" class=""></td>
            </tr>
        </tbody>
        <tbody>
            <tr>
                <td>
                    <div>
                        <input type="submit" name="mysubmit" value="Submit Post!">
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</form>

编辑:只需添加它以使其更易于搜索。该错误源于在 name 属性中使用句点。

【问题讨论】:

  • 如果用户必须为每个组检查一个按钮,那么您需要更改每个组的命名。您对所有人都使用相同的
  • 但是我将如何提供我需要的选项?例如,对于picture_6.tif 组,用户需要从其中一个单选按钮中进行选择(每个按钮都有不同的值)。其他组也一样。除非我遗漏了什么,否则更改名称不会提供该功能。
  • value="0" 解释为null 时出现问题,所以我们需要查看PHP 以找到确切原因
  • value 0 在 HTML 视图页面中被硬编码;更改为另一个值会导致相同的错误
  • 等等,现在我问你一个问题,如果第一组选择了picture_6.tif,第二组可以选择吗

标签: php html forms codeigniter radio-button


【解决方案1】:

您将您的radio 命名为name="picture_6.tif",并且在php 中您的参数可用$_POST["picture_6_tif"]。注意:不是$_POST["picture_6.tif"]。后者是未定义的 null。

<?php
var_dump($_POST);
?>
//array(5) { ["picture_7_tif"]=> string(1) "1" ["picture_6_tif"]=> string(1) "2" ["picture_8_tif"]=> string(1) "3" ["picture_9_tif"]=> string(1) "9" ["mysubmit"]=> string(12) "Submit Post!" } 

【讨论】:

  • 哇,好棒的收获!我完全不知道 POST 无法处理 . 并将其转换为 _
  • . 是 PHP 中的字符串连接运算符,因此 PHP 将接收到的表单参数 $_POST$_GET 等中的点转换为下划线。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-13
  • 2019-09-16
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
相关资源
最近更新 更多