Matt Elhotiby
Matt Elhotiby

Reputation: 44066

is there a way to pass two values in an input checkbox to the $_POST

I have a bunch of checkboxes on the page and i want to pass two values for each checkbox like this....

<input name="class[]" value="first_value" data="second_value" type="checkbox" class="auto"/>

any idea how to get first_value and second_value past to the $_POST in php

any suggestions on how to do this

Upvotes: 2

Views: 7590

Answers (4)

Matt Elhotiby
Matt Elhotiby

Reputation: 44066

You can do this

<input name="class[]" value="CIS 2910C DL:3" type="checkbox" class="auto"/>

where you separate the two values by a : or whatever other separator you want.

Then on the $_POST you can use explode in a loop like this

$pieces = explode(":", $class);
echo $pieces[0]; // CIS 2910C DL
echo $pieces[1]; // 3

so you can pull out both values

Upvotes: 7

DrFrow
DrFrow

Reputation: 86

How about:

<input name="class[second_value][]" value="first_value" type="checkbox" class="auto"/>

Then in PHP

foreach($_POST['class'] as $first_value=>$tmpArray) {
    foreach($tmpArray as $second_value) {
        echo $first_value.": ".$second_value;
    }
}

Odd way of doing it, but its seems like an odd situation you are in anyways.

Upvotes: 1

Matt Williamson
Matt Williamson

Reputation: 40193

You can't do it directly. I'd loop through with jquery and create new hidden inputs with those values and delete the data attr.

Upvotes: 2

user862010
user862010

Reputation:

I think you can only do this if you use ajax, or if you add a hidden field with an index sequential '[]' buying the checkbox with the value of X in X location hidden

Upvotes: 0

Related Questions