User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,024 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,690 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 15386 | Replies: 8
Reply
Join Date: Feb 2005
Posts: 4
Reputation: j-e is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
j-e j-e is offline Offline
Newbie Poster

collecting $_POST from a multiple select

  #1  
Feb 28th, 2005
Hi,

Just started picking up on PHP (from asp)... Going well. Like php a lot... except for........ My code is below... if someone can please help me get ALL the data from the $POST["catlist"] I would appreciate it.

Firstly: To ensure all the contents of "catlist" are selected, chuck this into the top.

<SCRIPT LANGUAGE=JavaScript>
function selectAll(box) {
for(var i=0; i<box.length; i++) {
box.options[i].selected = true;
}
}
</SCRIPT>

Secondly: The form

<form action="this.php" method="post" name=form>
select multiple size="10" name="catlist'>
<option value="3">Painter</option>
<option value="1">Database Designers</option>
<option value="2">Volunteer</option>
</select>
<input type="submit" name="btnSubmit" value="Submit" onClick="selectAll(this.form.catlist);">
</form>

Thirdly: this.php to capture the list into an array like this:
$catarray[0]=3
$catarray[1]=1
$catarray[2]=2

Here is what I have tried: (using php5)
$catarray= ($_POST['catlist']);
$catarray= array($_POST['catlist']);
other variations.

The most confusing is that Count($_POST['catlist'])=1


Please please help... sob... boo hooo... sob..
My brain hurts....

J-E
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Posts: 354
Reputation: DanceInstructor is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 12
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: collecting $_POST from a multiple select

  #2  
Feb 28th, 2005
[PHP]$catarray= $_POST['catlist'];
[/PHP]
shouldn't need parenthesis.

Have you tried:

[PHP]print_r($catarray);[/PHP]

I haven't pulled arrays from POST yet, so I'm not sure how it works. Here is a small script to output all the variables that are defined in php. You will be interested in the _POST section

[PHP]$output = get_defined_vars();
$fh = fopen('output.txt', at);
foreach($output as $yek => $out)
{
fwrite($fh, "*******************$yek****************************\r\n\r\n");
foreach($out as $key => $o)
{
fwrite($fh, "$key\t\t\t\t");
fwrite($fh, $o);
fwrite($fh, "\r\n\r\n");
}
}
fclose($fh);[/PHP]

That way you can be sure what the data looks like and if it is indeed being transeferred from the html form.

Dance
Clear Mind Hosting and Web Design

If I've helped you please consider adding to my reputation.
Reply With Quote  
Join Date: Feb 2005
Posts: 4
Reputation: j-e is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
j-e j-e is offline Offline
Newbie Poster

Re: collecting $_POST from a multiple select

  #3  
Feb 28th, 2005
Thanks for that... Unfortunately it seems there is no array coming through the POST. Only one value instead of 3.... any suggestions on how to get around it?

I will play around further.

J-E
Reply With Quote  
Join Date: Feb 2005
Posts: 354
Reputation: DanceInstructor is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 12
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: collecting $_POST from a multiple select

  #4  
Feb 28th, 2005
Found this example of an html form. Hope it helps.

<form action="handle_event.php" method="post">
<p> Event name: <input type="text" name="name" size="30" /></p>

<p>Week Days:
<input type="checkbox" name="weekdays[]" value="Sunday" />S
<input type="checkbox" name="weekdays[]" value="Monday" />M
<input type="checkbox" name="weekdays[]" value="Tuesday" />T
<input type="checkbox" name="weekdays[]" value="Wednesday" />W
<input type="checkbox" name="weekdays[]" value="Thursday" />T
<input type="checkbox" name="weekdays[]" value="Friday" />F
<input type="checkbox" name="weekdays[]" value="Saturday" />S
</p>

<input type="submit" name="submit" value="Add the Event!" />
</form>

It's from "PHP for the World Wide Web" by Larry Ullman http://www.dmcinsights.com/phpvqs2/

Dance
Clear Mind Hosting and Web Design

If I've helped you please consider adding to my reputation.
Reply With Quote  
Join Date: Feb 2005
Posts: 4
Reputation: j-e is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
j-e j-e is offline Offline
Newbie Poster

Re: collecting $_POST from a multiple select - SOLVED

  #5  
Feb 28th, 2005
Thanks a million. I appreciate your time. The solution makes perfect sense. Makes me wanna take up line dancing (sometimes).

Cheers

J-E
Reply With Quote  
Join Date: Feb 2005
Posts: 4
Reputation: j-e is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
j-e j-e is offline Offline
Newbie Poster

Re: collecting $_POST from a multiple select

  #6  
Feb 28th, 2005
Sideline comment No big deal.. it did break the "selectALL()" javascript (see point 1 of original post). But that should be a minor issue to fix.
Reply With Quote  
Join Date: Feb 2005
Posts: 354
Reputation: DanceInstructor is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 12
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: collecting $_POST from a multiple select

  #7  
Feb 28th, 2005
Yeah I just posted the example, didn't try to make it work for your situation, please excuse my laziness :mrgreen:

On a ranting note: I see the need for javascript, but HATE how PICKY it is.......

Dance
Clear Mind Hosting and Web Design

If I've helped you please consider adding to my reputation.
Reply With Quote  
Join Date: Nov 2007
Posts: 1
Reputation: jabdulius is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jabdulius jabdulius is offline Offline
Newbie Poster

Re: collecting $_POST from a multiple select

  #8  
Aug 6th, 2008
While that solution is cute, it is a workaround, and not a solution. I'm having the same issue, but my selections must be read from a DB; they are not static so defining checkboxes will not do.

Does anybody have a real solution to this problem?

Thanks!
Karim Varela
<URL SNIPPED>
Last edited by peter_budo : Aug 7th, 2008 at 3:08 pm. Reason: Keep It Spam-Free - Do not spam, advertise, plug your website, or engage in any other type of self promotion.
Reply With Quote  
Join Date: Jan 2007
Posts: 16
Reputation: mcd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
mcd mcd is offline Offline
Newbie Poster

Re: collecting $_POST from a multiple select

  #9  
Aug 6th, 2008
<disclaimer>Just took a look at this post again, and I'm not entirely sure that a multiple select won't post as a CSV value by default, so that's worth a go, but if you want to get down and dirty with some javascript, read on...</disclaimer>

Sure. You want to pass the selected values as a comma separated string. For the checkboxes, it's easy because the array is build for you and PHP reads it in no problem.

With the multiple selection SELECT element, you're going to have to make your javascript work a little harder.

Below, I have a function I wrote to parse a form for values. I use this to pass to an ajax call, but you could use it in a normal form submit as well, you would just have to read the parsed values from your multiple select into a hidden element.

But first the part you're interested in:

  1. var sels = form.getElementsByTagName('select'); // parse select elements
  2. for(var i=0;i<sels.length;i++)
  3. {
  4. q += '&' + sels[i].name + '='
  5. for (var j=0; j<sels[i].options.length; j++)
  6. if (sels[i].options[j].selected)
  7. q += escape(sels[i].options[j].value)+',';
  8. }

This is building a query string to pass in a GET, but you can easily modify it to build a CSV string.

Full function:
  1. function saveForm(form)
  2. {
  3. var q = (form.id ? '?key='+form.key.value : '?key='); //key is the primary key in the db
  4. var els = form.getElementsByTagName('input');
  5. for(var i=0;i<els.length;i++) //parse input elements
  6. {
  7. if(els[i].type=='checkbox')
  8. q += '&' + els[i].name + '=' + (els[i].checked==true ? els[i].value : '0');
  9. else
  10. q += '&' + els[i].name + '=' + escape(els[i].value);
  11. }
  12. var tels = form.getElementsByTagName('textarea'); //parse textarea elemtns
  13. for(var i=0;i<tels.length;i++)
  14. {
  15. q += '&' + tels[i].name + '=' + escape(tels[i].value);
  16. }
  17.  
  18. var sels = form.getElementsByTagName('select'); // parse select elements
  19. for(var i=0;i<sels.length;i++)
  20. {
  21. q += '&' + sels[i].name + '='
  22. for (var j=0; j<sels[i].options.length; j++)
  23. if (sels[i].options[j].selected)
  24. q += escape(sels[i].options[j].value)+',';
  25. }
  26.  
  27. sndReq('common/saveInd.php'+q,'sd'+form.count.value); // this would be a call to an ajax request
  28. }
Last edited by mcd : Aug 6th, 2008 at 11:01 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 1:45 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC