•
•
•
•
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
![]() |
•
•
Join Date: Feb 2005
Posts: 4
Reputation:
Rep Power: 0
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
<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:
Rep Power: 0
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 3: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:
Rep Power: 2
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 6th, 2008 at 11:01 pm.
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
Other Threads in the PHP Forum
- Previous Thread: simple sql query
- Next Thread: How do I add up all values of rows in a table???


Linear Mode