Page 1 of 1

Multiple Selection Box, Not receiving values

Posted: Sun Sep 14, 2008 6:48 pm
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.

Re: Multiple Selection Box, Not receiving values

Posted: Sun Sep 14, 2008 9:07 pm
by JAB Creations
Uh, what the heck is tutoring[]? 8O That shouldn't even validate.

Re: Multiple Selection Box, Not receiving values

Posted: Sun Sep 14, 2008 11:42 pm
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.

Re: Multiple Selection Box, Not receiving values

Posted: Mon Sep 15, 2008 12:26 am
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 ;)

Re: Multiple Selection Box, Not receiving values

Posted: Mon Sep 15, 2008 2:56 pm
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.

Re: Multiple Selection Box, Not receiving values

Posted: Mon Sep 15, 2008 7:39 pm
by alex.barylski
What does the echo'ed array look like?

Re: Multiple Selection Box, Not receiving values

Posted: Mon Sep 15, 2008 9:09 pm
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

Re: Multiple Selection Box, Not receiving values

Posted: Mon Sep 15, 2008 9:36 pm
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

Re: Multiple Selection Box, Not receiving values

Posted: Tue Sep 16, 2008 4:57 pm
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).