collecting $_POST from a multiple select

Reply

Join Date: Feb 2005
Posts: 4
Reputation: j-e is an unknown quantity at this point 
Solved Threads: 0
j-e j-e is offline Offline
Newbie Poster

collecting $_POST from a multiple select

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 355
Reputation: DanceInstructor is an unknown quantity at this point 
Solved Threads: 14
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: collecting $_POST from a multiple select

 
0
  #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 Quick reply to this message  
Join Date: Feb 2005
Posts: 4
Reputation: j-e is an unknown quantity at this point 
Solved Threads: 0
j-e j-e is offline Offline
Newbie Poster

Re: collecting $_POST from a multiple select

 
0
  #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 Quick reply to this message  
Join Date: Feb 2005
Posts: 355
Reputation: DanceInstructor is an unknown quantity at this point 
Solved Threads: 14
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: collecting $_POST from a multiple select

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

  1. <form action="handle_event.php" method="post">
  2. <p> Event name: <input type="text" name="name" size="30" /></p>
  3.  
  4. <p>Week Days:
  5. <input type="checkbox" name="weekdays[]" value="Sunday" />S
  6. <input type="checkbox" name="weekdays[]" value="Monday" />M
  7. <input type="checkbox" name="weekdays[]" value="Tuesday" />T
  8. <input type="checkbox" name="weekdays[]" value="Wednesday" />W
  9. <input type="checkbox" name="weekdays[]" value="Thursday" />T
  10. <input type="checkbox" name="weekdays[]" value="Friday" />F
  11. <input type="checkbox" name="weekdays[]" value="Saturday" />S
  12. </p>
  13.  
  14. <input type="submit" name="submit" value="Add the Event!" />
  15. </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 Quick reply to this message  
Join Date: Feb 2005
Posts: 4
Reputation: j-e is an unknown quantity at this point 
Solved Threads: 0
j-e j-e is offline Offline
Newbie Poster

Re: collecting $_POST from a multiple select - SOLVED

 
0
  #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 Quick reply to this message  
Join Date: Feb 2005
Posts: 4
Reputation: j-e is an unknown quantity at this point 
Solved Threads: 0
j-e j-e is offline Offline
Newbie Poster

Re: collecting $_POST from a multiple select

 
0
  #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 Quick reply to this message  
Join Date: Feb 2005
Posts: 355
Reputation: DanceInstructor is an unknown quantity at this point 
Solved Threads: 14
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: collecting $_POST from a multiple select

 
0
  #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 Quick reply to this message  
Join Date: Nov 2007
Posts: 1
Reputation: jabdulius is an unknown quantity at this point 
Solved Threads: 0
jabdulius jabdulius is offline Offline
Newbie Poster

Re: collecting $_POST from a multiple select

 
0
  #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 4: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 Quick reply to this message  
Join Date: Jan 2007
Posts: 16
Reputation: mcd is an unknown quantity at this point 
Solved Threads: 6
mcd mcd is offline Offline
Newbie Poster

Re: collecting $_POST from a multiple select

 
0
  #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 7th, 2008 at 12:01 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1
Reputation: rohan_shenoy is an unknown quantity at this point 
Solved Threads: 0
rohan_shenoy rohan_shenoy is offline Offline
Newbie Poster

Re: collecting $_POST from a multiple select

 
0
  #10
Dec 11th, 2008
I know this thread is old, but I feel it deserves this bump as I have the solution, which has not been given previously in this thread. Moreover this thread ranks well in search search, so I think the solution will benefit.

No need of any javascript hassles, instead use an array name for the 'select' field.

<select name="choices[]" multiple="multiple">
<option value="b">bold</option>
<option value="i">italic</option>
<option value="u">underline</option>
<option value="s">strike</option>
</select>

retreive it through(depending upoin GET/POST)
  1. $multiple_choices=$_POST['choices'];
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum


Views: 31054 | Replies: 12
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC