Solved

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

Posted on 2003-11-13
7
338 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now