普通网友 2014-09-18 05:22
浏览 28

PHP:如何检索具有相同名称的多个表单输入的值,?

I have a checkbox, and next to each checkbox I have a select option. I have multiple of these, and I want to retrieve the value of the select only if the checkbox is checked for it.

Here is the code:

<form method="POST">
<fieldset>
    <input type="checkbox" name="check1[]">
    <select name="select1[]">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select

<fieldset>
    <input type="checkbox" name="check1[]">
    <select name="select1[]">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select>
</fieldset>
<input type="submit" name="update" value="go">
</form>

Here is my PHP code:

if(isset($_POST['update'])){
    for($i = 0; $i < sizeof($_POST['check1']); $i++){
        if(isset($_POST['check1'][$i])){
            echo $_POST['select1'][$i];
        }
    }
}

This does not work, if the first checkbox is unchecked then the for loop returns the value of the second select option. Otherwise it works fine. How can i fix this?

Here is the array:

[check1] => Array
    (
        [0] => on
        [1] => on
    )

[select1] => Array
    (
        [0] => 1
        [1] => 3
    )
  • 写回答

2条回答 默认 最新

  • dongzhanyan3667 2014-09-18 06:45
    关注

    We get only checked check box on server that was your for loop is false over there.

    In this code i have add value to each check box to identify select box.

    eq: if check1[0] == 0 then we get select1[0]

    if check1[1] == 1 then we get select1[1]
    

    Sorry for my english.

    Html code

    <form action='' method="post">
        <fieldset>
            <input type="checkbox" name="check1[]" value='0'>
            <select name="select1[]">
                <option value="1">One</option>
                <option value="2">Two</option>
                <option value="3">Three</option>
            </select>
        </fieldset>
        <fieldset>
            <input type="checkbox" name="check1[]" value='1'>
            <select name="select1[]">
                <option value="4">One</option>
                <option value="5">Two</option>
                <option value="6">Three</option>
            </select>
        </fieldset>
        <input type="submit" name="update" value="go">
    </form>
    

    PHP code:

    if(isset($_POST['check1'])){
        for($i=0;$i<count($_POST['check1']);$i++){
            if($_POST['check1'][$i] >= 0){
                echo $_POST['select1'][$_POST['check1'][$i]];
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗