| | |
collecting $_POST from a multiple select
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Feb 2005
Posts: 4
Reputation:
Solved Threads: 0
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
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'])
=1Please please help... sob... boo hooo... sob..
My brain hurts....
J-E
[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
[/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
Found this example of an html form. Hope it helps.
It's from "PHP for the World Wide Web" by Larry Ullman http://www.dmcinsights.com/phpvqs2/
Dance
PHP Syntax (Toggle Plain Text)
<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
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
On a ranting note: I see the need for javascript, but HATE how PICKY it is.......
Dance
•
•
Join Date: Nov 2007
Posts: 1
Reputation:
Solved Threads: 0
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>
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.
•
•
Join Date: Jan 2007
Posts: 16
Reputation:
Solved Threads: 6
<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:
This is building a query string to pass in a GET, but you can easily modify it to build a CSV string.
Full function:
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:
javascript Syntax (Toggle Plain Text)
var sels = form.getElementsByTagName('select'); // parse select elements for(var i=0;i<sels.length;i++) { q += '&' + sels[i].name + '=' for (var j=0; j<sels[i].options.length; j++) if (sels[i].options[j].selected) q += escape(sels[i].options[j].value)+','; }
This is building a query string to pass in a GET, but you can easily modify it to build a CSV string.
Full function:
javascript Syntax (Toggle Plain Text)
function saveForm(form) { var q = (form.id ? '?key='+form.key.value : '?key='); //key is the primary key in the db var els = form.getElementsByTagName('input'); for(var i=0;i<els.length;i++) //parse input elements { if(els[i].type=='checkbox') q += '&' + els[i].name + '=' + (els[i].checked==true ? els[i].value : '0'); else q += '&' + els[i].name + '=' + escape(els[i].value); } var tels = form.getElementsByTagName('textarea'); //parse textarea elemtns for(var i=0;i<tels.length;i++) { q += '&' + tels[i].name + '=' + escape(tels[i].value); } var sels = form.getElementsByTagName('select'); // parse select elements for(var i=0;i<sels.length;i++) { q += '&' + sels[i].name + '=' for (var j=0; j<sels[i].options.length; j++) if (sels[i].options[j].selected) q += escape(sels[i].options[j].value)+','; } sndReq('common/saveInd.php'+q,'sd'+form.count.value); // this would be a call to an ajax request }
Last edited by mcd; Aug 7th, 2008 at 12:01 am.
•
•
Join Date: Dec 2008
Posts: 1
Reputation:
Solved Threads: 0
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.
retreive it through(depending upoin GET/POST)
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)
php Syntax (Toggle Plain Text)
$multiple_choices=$_POST['choices'];
![]() |
Other Threads in the PHP Forum
- Previous Thread: PHP Email Address validation through SMTP
- Next Thread: Help, please?
Views: 31054 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email error execution file files folder form forms function functions google href htaccess html htmlspecialchars image include insert integration ip java javascript joomla jquery limit link links login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote replace script search select server session sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube





