简体   繁体   中英

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.

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
    )

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]];
        }
    }
}

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. 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.

<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. If you want to be sure. Index your arrays ; This may help your php loop :

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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