Solved

* PROPER * reciept of select_multiple data sent with method='post'

Posted on 2003-11-13
7
355 Views
Last Modified: 2010-05-19
The php manual suggest naming the select someting like  name='theselect[]'  so the data will arrive in $_POST array as an array of the options selected.

It works, but it is not satisfying solution.
   name="letters[]" is not proper html as pr definition of cdata http://www.w3.org/TR/html4/types.html#type-cdata
   such names give trouble when you try to use them for javascript adressing of the form element.

browsers permit it and there are workarounds for the javascript adressing; but dammit, the must be some way to do it right.

with method='get' i have found the $_SERVER['QUERY_STRING'] but the amount is limited and anyway method='get' masticate the data, losing linefeeds et al.

I am beginning to suspect a conspiracy here :(

so before i go completly bonkers, would somebody please tell me hov i can get data from a mutiple select with a valid html name  say  
  <select name='nothopeless' multiple>

regards JakobA

0
Comment
Question by:JakobA
7 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9744969
hi,

hmm well i would say you should really be using the name as php suggests. whenever i've had to use javascript in conjunction with php array style names, i've just used a different way of accessing the elements as suggested by php (http://uk.php.net/manual/en/faq.html.php#faq.html.arrays). it works fine using variable = documents.forms[0].elements['theselect[]']; for example.. i can't think of how you would get the data out through POST or GET any other way

loz
0
 
LVL 6

Expert Comment

by:DoppyNL
ID: 9745788
if you don't want to use [] in your html you can number the fields manually:

name="field1"
name="field2"
name="field3"
name="field4"
name="field5"

you then have to adjust your loop to walk through these; it's harder to process the form this way, but it is still possible.
0
 
LVL 11

Accepted Solution

by:
shmert earned 250 total points
ID: 9749167
Use a regular POST form.  Put this in your php.ini file:

always_populate_raw_post_data = 1

Then you can access the raw post data from the request, and parse out the variables manually, using a function which appends the multiple vars to the $_POST array, and then merges the new $_POST into $_REQUEST.  This would be the most seamless way of doing it, I think.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 11

Expert Comment

by:shmert
ID: 9749328
Something like this should work for you:

<?php
function multipleRequestArguments() {
        $data = $GLOBALS['HTTP_RAW_POST_DATA'] . '&' . $_SERVER['QUERY_STRING'];
        echo $data . "<br />\n";
        $pairs = explode('&', $data);
        $out = array();
        foreach($pairs AS $pair) {
                if (!$pair) continue;
                list($key,$value) = explode('=', $pair);
                if (isset($out[$key])) {
                        if (!is_array($out[$key])) {
                                $out[$key] = array($out[$key], $value);
                        } else {
                                $out[$key][] = $value;
                        }
                } else {
                        $out[$key] = $value;
                }
        }
        return $out;
}
$MULTI_POST = multipleRequestArguments();
print_r($MULTI_POST);
?>
0
 
LVL 15

Author Comment

by:JakobA
ID: 9749578
>> schmert

Beautifull.  thank you

regards JakobA
0
 
LVL 11

Expert Comment

by:shmert
ID: 9749723
Thanks!  One important thing I forgot is calling urldecode() on all the $key and $value items.
0
 
LVL 15

Author Comment

by:JakobA
ID: 9749808
Right.  And if it that raw, i guess i better subtitite '_' for '.' in the type='image' parameter names.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question