View Full Version : checkboxes and variable variables into MySQL

10-06-2004, 08:04 PM

I am having some trouble with a form which consists of a number of courses, each of which has a checkbox to choose it. I have each checkbox formed with the following code:

<input type="checkbox" name="chosenClass[]" value="1" /> Course 1
<input type="checkbox" name="chosenClass[]" value="2" /> Course 2
<input type="checkbox" name="chosenClass[]" value="3" /> Course 3
<input type="checkbox" name="chosenClass[]" value="4" /> Course 4

The reason I chose to use the array as the name is because I got my javascript validation to work with it. (I know, it's sad :( ). It will alert the user if they have not checked at least one checkbox.

When the user hits 'submit', the data gets sent to the next php page (via POST). There, I try to extract the data from the stream like so:

foreach($_POST['chosenClass'] as $key => $value)
print $varname + " = $varname before";
print $varname + " = $key before";
print $varname + " = $value before";

$varname = "session".$key;
$$varname = $value;

print $varname + " = $varname after";
print $varname + " = $key after";
print $varname + " = $value after";
print "************";

The print statements are for testing.

What I get is this:

Does anyone have any idea of what I'm doing wrong? I'm using a variable variable.

Ideally, I wanted to just be able to capture a number or boolean value so that I can push this into a table in MySQL.


Much obliged to you all.

Dylan Leblanc
10-06-2004, 08:46 PM
You are getting all those zeros because you are adding strings together. Strings are supposed to be contatenated with the period.

10-12-2004, 04:40 PM
Whoops! I was thinking in java, not php. Thanks!