4

A form I don't have any control over is POSTing data to my PHP script. The form contains checkboxes along these lines:

<input type="checkbox" value="val1" name="option"/>
<input type="checkbox" value="val2" name="option"/>

If I were to write the code for the form, I'd write name="option[]" instead of name="option". But this is not a change I can do. Now, if both checkboxes are checked, $_POST["option"] returns just one of the values. How can I, in PHP retrieve all the values selected?

2
  • Any reason for the wiki? Commented Jul 3, 2009 at 2:04
  • Ian, you mean for enabling the community wiki? I also enable it, so people can improve on the question as they see fit. Commented Jul 8, 2009 at 19:40

1 Answer 1

14

You can read the raw post data. For example:

<fieldset>
    <legend>Data</legend>
    <?php
    $data = file_get_contents("php://input");
    echo $data."<br />";
    ?>
</fieldset>

<fieldset>
    <legend>Form</legend>
    <form method="post" action="formtest.php">
        <input type="checkbox" value="val1" name="option"/><br />
        <input type="checkbox" value="val2" name="option"/><br />
        <input type="submit" />
    </form>
</fieldset>

Check both boxes and the output will be:

option=val1&option=val2

Here's a live demo. All you have to do then is to parse the string yourself, into a suitable format. Here's an example of a function that does something like that:

function parse($data)
{
    $pairs = explode("&", $data);

    // process all key/value pairs and count which keys
    // appear multiple times
    $keys = array();
    foreach ($pairs as $pair) {
        list($k,$v) = explode("=", $pair);
        if (array_key_exists($k, $keys)) {
            $keys[$k]++;
        } else {
            $keys[$k] = 1;
        }
    }

    $output = array();
    foreach ($pairs as $pair) {
        list($k,$v) = explode("=", $pair);
        // if there are more than a single value for this
        // key we initialize a subarray and add all the values
        if ($keys[$k] > 1) {
            if (!array_key_exists($k, $output)) {
                $output[$k] = array($v);
            } else {
                $output[$k][] = $v;
            }
        } 
        // otherwise we just add them directly to the array
        else {
            $output[$k] = $v;
        }
    }

    return $output;
}

$data = "foo=bar&option=val1&option=val2";

print_r(parse($data));

Outputs:

Array
(
    [foo] => bar
    [option] => Array
        (
            [0] => val1
            [1] => val2
        )

)

There might be a few cases where this function doesn't work as expected though, so be careful.

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

2 Comments

Depending on your PHP settings you may not be able to use file_get_contents on the php://input stream. In this case, you will need to use fopen('php://input', 'r') and stream_get_contents($fp)
What PHP settings would that be?

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.