【问题标题】:Fetch data from database into checkbox and get the selected values PHP [duplicate]从数据库中获取数据到复选框并获取选定的值 PHP [重复]
【发布时间】:2020-05-02 17:41:51
【问题描述】:

我是 PHP 和 Mysql 的新手。我在 Mysql 中有一个名为“has”的表,用于存储客户的物理对齐方式。有两个属性 CustomerID 和 PyhsicalAilmentName 。在注册屏幕中,我希望用户在复选框中选择它们。我可以使用此表单代码将数据库中的物理对齐方式提取到复选框中。

   <form action="includes/signup.inc.php"  style="border:1px solid #ccc;width: 50%;margin: 0 auto" method="post" >
            <div class="container" >
            <h1>Sign up</h1>
            <p>Please fill in this form to create an account.(Your username should start with "C_")</p>

                <hr>
            <input type="text" name="username" placeholder="Name">
            <input type="text" name="user_last_name" placeholder="Last Name">
            <input type="text" name="uid" placeholder="Username">
            <input type="password" name="pwd" placeholder="Password">
            <input type="password" name="pwd-repeat" placeholder="Repeat Password">
            <input type="text" name="user_weight" placeholder="Weight(in terms of kilogram)">
            <input type="text" name="user_length" placeholder="Length(in terms of cm)">
            <input type="text" name="user_age" placeholder="Age">

                <p> Phsical Alignments</p>
                <?php
                    $sql = "select Name from physical_ailment";
                    $result = mysqli_query($conn,$sql);
                    $i = 0;

                    while($db_row = mysqli_fetch_array($result)){
                        ?>
                        <input type="checkbox" name="check_list[]"> <?php
                            echo $db_row["Name"]; ?> <br>
                        <?php
                        $i++; }
                        ?>


            <select name="selected_mem_type" >

                <?php
                    $sql = "select Name from membership_type";
                    $result = mysqli_query($conn,$sql);
                    $i = 0;

                    while($DB_ROW = mysqli_fetch_array($result)){
                        ?>
                        <option>
                            <?php echo $DB_ROW['Name']; ?>
                        </option>
                        <?php
                            $i++;} ?>
            </select>
            <input type ="text" name="user_card_no" placeholder="Credit Card No">
            <input type="date" name="user_card_expire_date" placeholder="Expire Date of Card">
            <button type="submit" class="signupbtn" name="signup-submit"> Signup </button>
                <button type="submit" class="signupbtn" name="home-submit"> Home </button>
            </div>
        </form>

问题是当我打算使用$_POST['check_list']via foreach 循环获取选定的复选框时,它会根据选定复选框的数量打印 "on"。如果用户在 $_POST['check_list'] 中选择 3 个复选框,则有 3 个元素是 "on" 。当我选择两件事时,让我们说,并用print_r 打印$_POST['check_list'] 它输出Array ( [0] =&gt; on [1] =&gt; on [2] =&gt; on ) 我搜索了很多,但无法找到解决方案。感谢您的帮助,感谢您的关注。

【问题讨论】:

  • 经过 10 年的内容生成和数百万个问题,大多数基本编程问题至少有 5 页来表达如何解决给定问题。在提出新问题之前,请在寻找解决方案时多花点功夫。

标签: php html mysql checkbox


【解决方案1】:

如果您没有为 HTML 中的复选框提供 value 属性,则在大多数浏览器中默认为 on 以通知您它已被选中。

因此,如果您要制作复选框,要求人们检查他们最喜欢的 3 种水果。

<input type="checkbox" name="check_list[]"> Banana <br>
<input type="checkbox" name="check_list[]"> Apple <br>
<input type="checkbox" name="check_list[]"> Orange <br>

如果所有 3 个都被选中,您将拥有Array ( [0] =&gt; on [1] =&gt; on [2] =&gt; on )

现在如果你添加 value 属性

<input type="checkbox" name="check_list[]" value="banana"> Banana <br>
<input type="checkbox" name="check_list[]" value="apple"> Apple <br>
<input type="checkbox" name="check_list[]" value="orange"> Orange <br>

如果所有 3 个都被选中,您将拥有: Array ( [0] =&gt; banana [1] =&gt; apple [2] =&gt; orange )

您可以在此处阅读更多相关信息:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Value

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    相关资源
    最近更新 更多