Reading result from Mulitple ListBox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
terji
Forum Commoner
Posts: 37
Joined: Tue May 14, 2002 5:27 pm
Location: Denmark

Reading result from Mulitple ListBox

Post by terji »

I'm having trouble reading results from a listbox like this one:
<select name="listbox" size="6" multiple>

I get the result if only one subject is selected, but when I select more than one I seem to get a random one instead of all I selected.


How can I read all the subjects selected in the listbox:?:
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

In your PHP, use the print_r() function on the listbox variable.

So, if your listbox variable was called $listbox, do this:

Code: Select all

echo "<pre>";
print_r($listbox);
die();
This will show you how to access them.
terji
Forum Commoner
Posts: 37
Joined: Tue May 14, 2002 5:27 pm
Location: Denmark

Post by terji »

I've tried using the code mentioned:

echo "<pre>";
print_r($listbox);
die();

But all it shows is a 'blank' page with this code:

<pre>11

where 11 is one of the 3 items I chose. What am I doing wrong?!?!?


:cry:
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

So thats a blank page with text on it :D
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Ahh....yes, here is the answer, it's a bugger for sure:

http://www.phpbuilder.com/columns/tim19991124.php3
denisb
Forum Newbie
Posts: 2
Joined: Tue May 14, 2002 8:33 am

Post by denisb »

Try next code:
<select name="listbox[]" size="6" multiple>
sycodon
Forum Newbie
Posts: 1
Joined: Sat Jun 01, 2002 10:10 am

Watch out for the 0 index!

Post by sycodon »

As a new PHP person I have just figured out what's been kicking my ass for the last 2 weeks. The index elements of an array in PHP start with 0, not 1.

'C' guys probaly figured this out, but I'm a COBOL/Powerbuilder guy.


Hope this saves someone weeks of effort.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

If you are every unsure what exactly a variable contains:

Code: Select all

&lt;?php
function debug_var ( $variable )
{
    echo "&lt;pre&gt;";
    var_dump($variable);
    echo "&lt;/pre&gt;";
}
?&gt;
And using that function, just call it like this:

Code: Select all

&lt;?php
debug_var($whatever_variable_you_want);
?&gt;
Post Reply