Guillaume
Guillaume

Reputation: 352

How to get multiple values through one check box?

I would like to get an array from a checkbox with one [id] and one [date] for each record.

Here is my actual HTML / PHP :

<input type="checkbox" name="collection[][id]" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="collection[][date]" value="<?php echo date(Ymd); ?>" />

I get this :

Array
(
    [0] => Array
        (
            [id] => 544826
        )

    [1] => Array
        (
            [date] => 20170426
        )

    [2] => Array
        (
            [id] => 608555
        )

    [3] => Array
        (
            [date] => 20170426
        )
)

And I would like this :

Array
(
    [0] => Array
        (
            [id] => 544826
            [date] => 20170426
        )

    [1] => Array
        (
            [id] => 608555
            [date] => 20170426
        )
)

Please, how could I proceed ?

Upvotes: 0

Views: 2264

Answers (4)

JYoThI
JYoThI

Reputation: 12085

you need to give same index i.e key for both id and date while push the value like this

<input type="checkbox" name="collection[<?php echo $row["id"]; ?>][id]" value="<?php echo $row["id"]; ?>" />
<input type="hidden" name="collection[<?php echo $row["id"]; ?>][date]" value="<?php echo date(Ymd); ?>" />

Upvotes: 2

Sahil Gulati
Sahil Gulati

Reputation: 15141

Just copy paste this code, I have tested it and this will surely help you achieve what you want.

<form>
    <input type="checkbox" name="collection[<?php echo $row["id"]; ?>][id]" value="<?php echo $row["id"]; ?>" />
    <input type="hidden" name="collection[<?php echo $row["id"]; ?>][date]" value="<?php echo date(Ymd); ?>" />
    <input type="checkbox" name="collection[<?php echo $row["id"]; ?>][id]" value="<?php echo $row["id"]; ?>" />
    <input type="hidden" name="collection[<?php echo $row["id"]; ?>][date]" value="<?php echo date(Ymd); ?>" />
    <input type="submit">
</form>

<?php

print_r(array_values($_GET["collection"]));

Output:

  Array
    (
        0 => Array
            (
                [id] => 10
                [date] => 20170426
            )

        1 => Array
            (
                [id] => 20
                [date] => 20170426
            )

    )

Upvotes: 1

Why don't you use the ID as the index of the checkbox? That way you just need to go trough the list. i.e:

<input type="checkbox" name="collection[<?php echo $row['id']; ?>]" value="<?php echo date(Ymd); ?>" />

So that way you'll have the following array in collenction (for the ones ticked):

$collection = [
'id1' => 'date1',
'id2' => 'date2',
...
]

Simpler and more elegant. Hope this helps

Upvotes: 1

Heshan Nanayakkara
Heshan Nanayakkara

Reputation: 21

From your comments, it sounds like you have some JavaScript that handles the data before it's submitted. If that's the case, you can add a data attribute to the checkbox. To use your example, you could call it data-valuetwo.

input type="checkbox" value="testuser" data-valuetwo="1">

Then, your JavaScript can use getAttribute to retrieve the value in your data-valuetwo attribute and handle it appropriately. It could look something like this:

var valuetwo = checkbox.getAttribute("data-valuetwo");

Upvotes: 1

Related Questions