0

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.

1
  • Ideally you want to name the checkboxes that end in a [] so that you'll get an array in PHP. You can't change the name the checkboxes are given? Commented Jun 28, 2013 at 20:43

3 Answers 3

1

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]

Sign up to request clarification or add additional context in comments.

3 Comments

I understand that what I have above is incorrect markup, but like I said, this is a 3rd party tool. When the data is submitted, Firebug shows multiple instances of 'V25' in the post data that is sent. Is there no way to retrieve it?
Well, using javascript and hardcoding - yes :D
While the info in this answer is correct, it doesn't accurately address the original question. After researching a bunch, I was able to come up with a solution that works with the situation described.
0

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")+"[]");
});  

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.