Multiple Selection Box, Not receiving values

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
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Multiple Selection Box, Not receiving values

Post by Trenchant »

I'm putting together an online registration form for a client. Here's what it looks like:
http://poptutoring.com/test.php

The problem is that I can't receive the values from the selection box's at the bottom. Here's the code:

Code: Select all

$tutoring = mysql_real_escape_string(strip_tags($_POST['tutoring']));
foreach($tutoring as $value_t) {
    echo $value_t;
}
    
if (is_array($tutoring)) {
    echo "success";
}

Code: Select all

Tutoring:<br /><select class="look" size="3" id='tutoring' name='tutoring[]' multiple><option value='One on One'>One on One</option><option value='One on One'>Group</option><option value='One on One'>Math & Physics Centers</option></select>
 
 
This is really beyond me why it isn't working. I've checked various other examples and they seem to be the same. Does anyone know what I'm doing wrong because I can't see it.

This script says:
Warning: Invalid argument supplied for foreach() in C:\AppServ\www\Projects\pop\test.php on line 207
and it doesn't give me a "success" saying its an array.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Multiple Selection Box, Not receiving values

Post by JAB Creations »

Uh, what the heck is tutoring[]? 8O That shouldn't even validate.
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Re: Multiple Selection Box, Not receiving values

Post by Trenchant »

changing it to "tutoring" doesn't help. All the tutorials said it had to be set to [] with PHP otherwise it wouldn't create an array or it would create a string with the name "array" or something along those lines.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Multiple Selection Box, Not receiving values

Post by alex.barylski »

Uh, what the heck is tutoring[]? That shouldn't even validate.
I do that all the time and my code validates just fine? :?

OP: Try a dump of the variables:

Code: Select all

echo '<pre>';
print_r($_POST);
echo '</pre>';
The variable $_POST['tutoring'] should contain the array of selected items.

p.s-Drop the mysql_real_escape_string & strip_tags calls at that point...they work on strings not arrays ;)
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Re: Multiple Selection Box, Not receiving values

Post by Trenchant »

Alright I removed the filter and now its echoing the last entry from $_POST.

It still doesn't recognize it as an array and the for each statement doesn't work.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Multiple Selection Box, Not receiving values

Post by alex.barylski »

What does the echo'ed array look like?
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Re: Multiple Selection Box, Not receiving values

Post by Trenchant »

The line below INC: is what the echo is. Feel free to test it out. I have it set so nothing will be inserted into the database yet. All text fields must be filled and the tutoring selection box is the only one configured.

http://poptutoring.com/test.php

The only thing $_POST sends back is the last entry in Tutoring
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Multiple Selection Box, Not receiving values

Post by alex.barylski »

I filled/selected every select box and Math & Physics was returned...I then looked at your HTML source and I can see that you no longer specifiy an array like so:

tutoring[] NOT tutoring

Add those brackets and try again, this time all three should show up.

Actually...hang on...just checked your source again and I notice that you do not specify any values for each <option> tag...you need to do that otherwise you will get wonky results, so something like this should work:

Code: Select all

<select name="tutoring[]" multiple="multiple">  <option value="Math & Physics">Math & Physics</option>  <option value="Math & Physics">Computer Science</option>  <option value="Math & Physics">English</option></select>
p.s-I like your design work...you available for contract design? Your in Regina? I'm sorry for that... :P
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Re: Multiple Selection Box, Not receiving values

Post by Trenchant »

Awesome it's working now. I don't work much with arrays and never even thought mysql_real_escape_string might be preventing the values from going through.

I've changed the filter to work like this:

Code: Select all

foreach($_POST['tutoring'] as $value_c) {
    $value_c = mysql_real_escape_string(strip_tags($value_c));
}
It's working great now.

BTW, I used to design my own websites but it wasn't affordable and it was very time consuming. I now employ a company in India that does all the design work. I meet with the client in person and go over everything about their website. I then make a rough sketch of the site and send it to the designer in India. They put everything together and send me the template. From there I assemble the website and do any PHP work.

I'm not really wanting to go into any more detail publically. If anyone wants to start doing something like this just give me a shout on the forums here. It's a great way for students to make some extra income. Right now it pays my expenses(I'm currently at the University of Regina taking Pre-Medicine).
Post Reply