简体   繁体   English

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

[英]PHP: How to retrieve values of multiple form inputs with the same name,?

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. 我有多个,并且仅在选中复选框时才想检索select的值。

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: 这是我的PHP代码:

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. 这不起作用,如果未选中第一个复选框,则for循环将返回第二个选择选项的值。 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
    )

We get only checked check box on server that was your for loop is false over there. 我们仅在服务器上选中了您的for循环在那边为false的复选框。

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] eq:如果check1 [0] == 0,则得到select1 [0]

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

Sorry for my english. 对不起我的英语不好。

Html code HTML代码

<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: PHP代码:

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

I would do otherwise. 否则我会做。 When the checkbox is checked, in javascript I would activate the select input and retrieve eventually the result in a hidden field. 选中此复选框后,在javascript中,我将激活选择输入并最终在隐藏字段中检索结果。 You could also change the value of the checked input to 1 only when it is activated and link it to an hidden input field. 您还可以仅在选中输入时将其更改为1并将其链接到隐藏的输入字段。

<input type="checkbox" name="check1[]">
<input type="hidden" name="hiddenValue[]" value="0">
    <select name="select1[]">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select

You have to understand that only checked input are send to the server so your array for check1[] will be different from the one you expected. 您必须了解,只有已检查的输入才发送到服务器,因此check1 []的数组将与您期望的数组不同。 If you want to be sure. 如果您想确定的话。 Index your arrays ; 索引数组; This may help your php loop : 这可以帮助您的php循环:

<input type="checkbox" name="check1[0]">
<input type="checkbox" name="check1[1]">

By the way my select input would begin with an empty option and no checkbox. 顺便说一句,我的选择输入将以一个空选项和没有复选框开始。 checking the empty value for an option is easier. 检查选项的空值更加容易。 But may be you need this checkbox so it was just an info. 但是可能您需要此复选框,所以它只是一个信息。

<select name="select1[]">
        <option value="0">Choose an option</option>
        <option value="4">One</option>
        <option value="5">Two</option>
        <option value="6">Three</option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM