Jerbot
Jerbot

Reputation: 1168

HTML multiple checkboxes with identical name= into PHP $_POST

So I have a 3rd party survey tool that generates html surveys, and for multi-select checkboxes in a given question, it will name them all the same:

<input type="checkbox" value="1" id="V25_1" name="V25">
<input type="checkbox" value="2" id="V25_2" name="V25">

Is there a way that all the selected values can be retrieved in PHP? $_POST by default seems to simply store only the last selected value.

Upvotes: 0

Views: 269

Answers (3)

Jerbot
Jerbot

Reputation: 1168

It is possible to retrieve all variables when you have multiple elements with identical 'name' parameters. After much scouring the internet for a solution, I wrote the following php script which grabs the raw post data, and parses it:

<?php
    function RawPostToArray() {
        $rawPostData = file_get_contents("php://input");
        if($rawPostData) {
            $rawPostData = explode('&', $rawPostData);

            if($rawPostData && is_array($rawPostData) && count($rawPostData)) {
                $result = array();
                foreach($rawPostData as $entry) {
                    $thisArray = array();
                    $split = explode('=', urldecode($entry), 2);

                    $value = (count($split) == 2) ? $split[1] : '';

                    if(array_key_exists($split[0], $result)) {
                        if(is_array($result[$split[0]])) {
                            $result[$split[0]][] = $value;
                        }
                        else {
                            $thisArray[] = $result[$split[0]];
                            $thisArray[] = $value;
                            $result[$split[0]] = $thisArray;
                        }
                    }
                    else {
                        $result[$split[0]] = $split[1];
                    }
                }
                return $result;
            }
            else return array();
        }
        else return array();
    }
?>

Any duplicate 'names' are bumped down into an array within the result, much the way $_POST does, when the HTML is coded correctly. There is probably plenty of room for improvement here, and I'm sure not all situations are accounted for, but it works well for my application.

Suggestions are appreciated where improvements can be made.

Upvotes: 0

Tomas Bulva
Tomas Bulva

Reputation: 1220

You could run something like this before the form submit:

$(":checkbox").each(function(){
   $(this).attr("name",$(this).attr("id"));
});

or

$(":checkbox").each(function(){
   $(this).attr("name",$(this).attr("name")+"[]");
});  

Upvotes: 0

sybear
sybear

Reputation: 7784

Your code should be approximately the following:

<select multiple="multiple">
  <input type="checkbox" value="1" id="V25_1" name="V25[]">
  <input type="checkbox" value="2" id="V25_2" name="V25[]">
</select>

V25[] means that you can get the value from an array. e.g. $_GET['V25'][0]

You could also specify an index if needed:

V25[1] or V25[a]

Upvotes: 1

Related Questions