tags:

views:

73

answers:

2

Hi

I have a form that passes something like in a URL

?choice=selection+A&choice=selection+C

I'm collecting it in a form with something like (I remember that $_GET is any array)

     $temp = $_GET["choice"];
 print_r($temp);

I'm only getting the last instance "selection C". What am I missing

+5  A: 

I am assuming 'choice' is some kind of multi-select or checkbox group? If so, change its name in your html to 'choice[]'. $_GET['choice'] will then be an array of the selections the user made.

Brad
You would definetly use this method when you want to collect multiple options of the same group (like any array of checkboxes or a select that allows multiple choices)
Kevin Peno
For reference: http://www.php.net/manual/en/faq.html.php#faq.html.arrays
outis
+1  A: 

If you don't intend to edit the HTML, this will allow you to do what you're looking to do; it will populate the $_REQUEST superglobal and overwrite its contents.

This assumes PHP Version 5.3, because it uses the Ternary Shortcut Operator. This can be removed.

$rawget = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : false;
$rawpost = file_get_contents('php://input') ?: false;
$target = $rawget;
$_REQUEST = array();
if ($target !== false) {
    $pairs = explode('&',$rawget);
    foreach($pairs as $pair) {
        $p = strpos($pair,'=');
        if ($p === false && !empty($pair)) {
            $_REQUEST[$pair] = null;
        }
        elseif ($p === 0 || empty($pair)) {
            continue;
        }
        else {
            list($name, $value) = explode('=',$pair,2);
            $name = preg_replace('/\[.*\]/','',urldecode($name));
            $value = urldecode($value);
            if (array_key_exists($name, $_REQUEST)) {
                if (is_array($_REQUEST[$name])) {
                    $_REQUEST[$name][] = $value;
                }
                else {
                    $_REQUEST[$name] = array($_REQUEST[$name], $value);
                }
            }
            else {
                $_REQUEST[$name] = $value;  
            }
        }
    }
}

As it stands, this will only process the QueryString/GET variables; to process post as well, change the 3rd line to something like

$target = ($rawget ?: '') . '&' . ($rawpost ?: '');

All that having been said, I'd still recommend changing the HTML, but if that's not an option for whatever reason, then this should do it.

Dereleased