【问题标题】:How to get values of multiple selected (dynamic) checkbox in php?如何在php中获取多个选定(动态)复选框的值?
【发布时间】:2014-12-01 11:09:10
【问题描述】:

我是初学者。我在一张桌子上显示了 10 个学生的学生 ID 和学生姓名。针对每个学生 ID,应该有一个 checkbox(动态)。当我点击添加按钮时,所有选中的学生详细信息(id、姓名)都必须插入另一个databasetable。我该怎么办?

【问题讨论】:

  • 到目前为止你做了什么?

标签: php jquery mysql checkbox


【解决方案1】:
<form method="post" action="pageurl">
        <input type="checkbox" name="studentid[]" value="1,Student1" />Student1
        <input type="checkbox" name="studentid[]" value="2,Student2" />Student2
        <input type="checkbox" name="studentid[]" value="3,Student3" />Student3
        <input type="checkbox" name="studentid[]" value="4,Student4" />Student4

        <input type="submit" />
        </form>
        <?php

        $id=$_POST['studentid'];
        foreach($id as $student)
        {
        $extract = explode(',',$student);
        $query="INSERT INTO student (id,name) values ('$extract[0]','$extract[1]')";
        }

        ?>

【讨论】:

【解决方案2】:

使用复选框名称作为数组, 示例:

<form method="post" action="" id="frm_id">
        <input type="checkbox" name="chkid[]" value="10,Anu" />Anu
        <input type="checkbox" name="chkid[]" value="11,Raj" />Raj
        <input type="checkbox" name="chkid[]" value="12,Ram" />Ram
        <input type="checkbox" name="chkid[]" value="13,xxx" />xxx
        <input type="checkbox" name="chkid[]" value="14,yyy" />yyyy
        <input type="checkbox" name="chkid[]" value="15,zzz" />zzz
        <input type="checkbox" name="chkid[]" value="16,qqqq" />qqqq
        <input type="submit" value="Insert"  name="sub"/>
        </form>
        <?php
        if(isset($_POST['sub']))
        {
        $id=$_POST['chkid'];
        for($i=0;$i<count($id);$i++)
        {
        $exp=explode(',',$id[$i]);//Explode id and name
        echo 'id='.$exp[0].',Name='.$exp[1];echo "<br>";
        echo $query="INSERT INTO tbl_student (id,name) values ('$exp[0]','$exp[1]')";echo "<br><br>";
        }
        }
        ?>

【讨论】:

  • 我认为我对复选框 。错了吗?
  • 危险:你很容易受到SQL injection attacks的影响,你需要defend你自己。
  • @MIA 使用 php 标签,
【解决方案3】:

尝试使用这样的复选框元素数组:

<input type="checkbox" name="months[]" value="feb">February<br>
<input type="checkbox" name="months[]" value="mar">March<br>
<input type="checkbox" name="months[]" value="apr">April<br>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2019-12-20
    • 2015-02-11
    • 2023-03-04
    • 1970-01-01
    • 2021-07-30
    相关资源
    最近更新 更多