im trying to send the checkbox data to both the email and to the database but for some reason it doesnt send all the of the checked items to the email or database what am i doing wrong?

here is the form:

<table width="95%" border="0" cellspacing="1" class="tablestyle" cellpadding="1" align="center">

<tr><td>Name:</td>
<td><input type="text" name="name" size="27">
</td>
</tr>

<tr> 
<td>Email: </td>
<td> 
<input type="text" name="email" size="27">
</td>
</tr>


<tr> 
<td>Address: </td>
<td> 
<input type="text" name="address" size="27">
</td>
</tr>

<tr> 
<td>Home Phone: </td>
<td> 
<input type="text" name="homephone" size="27">
</td>
</tr>

<tr> 
<td>Alternate Phone: </td>
<td> 
<input type="text" name="alternatephone" size="27">
</td>
</tr>

<tr> 
<td valign="top">Please check all volunteer activities you would like to do: </td>
<td> 

Website/Newsletter:<input type="checkbox" value="website_newsletter" name="activities[]"><br />
Clothing Bank:<input type="checkbox" value="clothing_bank" name="activities[]"><br />
Food Drives:<input type="checkbox" value="food_drives" name="activities[]"><br />

</td>
</tr>

<tr> 
<td valign="top">Help With Special Events: </td>
<td> 

Teasure Sale:<input type="checkbox" value="Teasure Sale" name="specialevents[]"><br />
Volunteer Celebation:<input type="checkbox" value="Volunteer Celebation" name="specialevents[]"><br />
Fun/Run:<input type="checkbox" value="Fun/Run" name="specialevents[]"><br />
Children's Parade:<input type="checkbox" value="Children's Parade" name="specialevents[]"><br />

</td>
</tr>

<tr> 
<td valign="top">My Best Available Times are: </td>
<td> 

<input type="text" name="best_time" size="27">

</td>
</tr>

<tr><td colspan="2"><input type="submit" value="Submit" class="button"></td></tr>
</table></form>

here is the commands:

<?php
$con = mysql_connect("host","user","password"); 
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("t", $con); 
$name=mysql_real_escape_string($_POST['name']); 
$email=mysql_real_escape_string($_POST['email']);
$address=mysql_real_escape_string($_POST['address']); 
$homephone=mysql_real_escape_string($_POST['homephone']); 
$alternatephone=mysql_real_escape_string($_POST['alternatephone']); 
$activities=mysql_real_escape_string($_POST['activities']); 
$specialevents=mysql_real_escape_string($_POST['specialevents']); 
$best_time=mysql_real_escape_string($_POST['best_time']); 
$sql="INSERT INTO t (name,email,address,homephone,alternatephone,activities,specialevents,best_time) VALUES ('$name','$email','$address','$homephone','$alternatephone','$activities','$specialevents','$best_time')"; 
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
 die('Error: ' . mysql_error());
}
?>
<?php
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$address = trim($_POST['address']);
$homephone = trim($_POST['homephone']);
$alternatephone = trim($_POST['alternatephone']);
$activities = trim($_POST['activities']);
$specialevents = trim($_POST['specialevents']);
$best_time = trim($_POST['best_time']);


echo" <br />";
echo" <br />";
echo" <br />";
echo "Name: ". $name . " <br />";
echo "Email: ". $email . " <br />";
echo "Address: ". $address . " <br />";
echo "Home Phone: ". $homephone . " <br />";
echo "Alternate Phone: ". $alternatephone . " <br />";
echo "Activities: ". $activities . " <br />";
echo "Special Events: ". $specialevents . " <br />";
echo "Best Time Available: ". $best_time . " <br />";


$recipient = "email";
$subject   = "Volunteering form";
$message   = " 


'. Name: .'$name 

'. Email: .' $email 

'. Address: .' $address 

'. Home Phone: .' $homephone 

'. Alternate Phone: .' $alternatephone 

'. Activites: .' $activities 

'. Special Events: .' $specialevents 

'. Best Time: .' $best_time 

";

mail($recipient, $subject, $message);
?>

what am i doing wrong? ive been googling tutorials but it doesnt seem to work for me. ive been trying for hours and i still dont get it please help?

You are taking the checkbox array and inserting it to the database. First, you have to 'collect' the selected checkboxes, because, unchecked checkboxes values aren't posted.
To see what you selected, use a for/foreach loop.
Eg.

foreach($_POST['checkboxname'] as $selected_value ) {
echo $selected_value."<br />";
//do something
}

$selected_value print only those checkboxes which were selected. Do whatever you want with it! :)
I hope its clear ?

okay i got the selected choices to appear with the

foreach($_POST['activities'] as $selected_value ) {
 echo $selected_value."<br />";
      //do something
      }

so say now i wanted the selected checkboxes values to be sent to my email i would just use the "$selected_value" and it will show right?

but what about my database? how do i send the selected values to my data base i replaced the $activities with $selected values but it doesnt show the selected checkboxes on my database did i do something wrong?:

$con = mysql_connect("host","user","pass"); //Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("volunteer", $con); //Replace with your MySQL DB Name
$name=mysql_real_escape_string($_POST['name']); //This value has to be the same as in the HTML form file
$email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file
$address=mysql_real_escape_string($_POST['address']); //This value has to be the same as in the HTML form file
$homephone=mysql_real_escape_string($_POST['homephone']); //This value has to be the same as in the HTML form file
$alternatephone=mysql_real_escape_string($_POST['alternatephone']); //This value has to be the same as in the HTML form file
$selected_value=mysql_real_escape_string($_POST['activities']); //This value has to be the same as in the HTML form file
$specialevents=mysql_real_escape_string($_POST['specialevents']); //This value has to be the same as in the HTML form file
$best_time=mysql_real_escape_string($_POST['best_time']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO volunteer (name,email,address,homephone,alternatephone,activities,specialevents,best_time) VALUES 

('$name','$email','$address','$homephone','$alternatephone','$selected_value','$specialevents','$best_time')"; /*form_data is the 

name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
 die('Error: ' . mysql_error());
}

You are treating specialevents as one value, when it is actually an array. What I would do would collect all values of the array and shove them into one variable:

$selectedevents='';
foreach($_POST['selectedevents'] as $val ) {
$selectedevents .= $val.", ";
}
$selectedevents=substr($selectedevents,0,-2);

Now the variable $selectedevents is ready to go into the db.

How do you want to insert selected checkbox values ? You can insert all the selected values in 1 field as buddylee17 has mentioned or you can have one row for 1 selected value. It all depends on how you want to save the values.

im trying to have it insert to the database to be something like: event1, event2

what do i need to name the table in the database? instead of activities to i have to rename it selected_value? and is that the same goes for the form? do i change it to selected value also? as of now when i use the $selected_value it doesnt show the selected values in the database. what type of table am i suppose to make? i just made a varchar is that okay?

i also need to know the selected value from the email but when i use $selected_value it doesnt show up on the email.

anyways i did what you suggested but it still doesnt work unless i didnt do it right? sorry i know i dont have a very good knowledge on this kind of stuff.

<?php
$con = mysql_connect("host","user","pass"); //Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("volunteer", $con); 
$name=mysql_real_escape_string($_POST['name']); 
$email=mysql_real_escape_string($_POST['email']); 
$address=mysql_real_escape_string($_POST['address']); 
$homephone=mysql_real_escape_string($_POST['homephone']); 
$alternatephone=mysql_real_escape_string($_POST['alternatephone']); 
$[U]selected_value[/U]=mysql_real_escape_string($_POST['[U]activities[/U]']);
$specialevents=mysql_real_escape_string($_POST['specialevents']); 
$best_time=mysql_real_escape_string($_POST['best_time']); 
$sql="INSERT INTO volunteer (name,email,address,homephone,alternatephone,[U]activities[/U],specialevents,best_time) VALUES 

('$name','$email','$address','$homephone','$alternatephone','[U]$selected_value[/U]','$specialevents','$best_time')"; /*form_data is the 

name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
 die('Error: ' . mysql_error());
}
?>

<?php
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$address = trim($_POST['address']);
$homephone = trim($_POST['homephone']);
$alternatephone = trim($_POST['alternatephone']);
$activities = trim($_POST['activities']);
$specialevents = trim($_POST['specialevents']);
$best_time = trim($_POST['best_time']);

[U]$selected_value='';
foreach($_POST['selecte_value'] as $val ) {
$selected_value .= $val.", ";
}
$selectedevents=substr($selected_value,0,-2);[/U]


echo "Thank you $name for volunteering!";

echo" <br />";
echo" <br />";
echo" <br />";
echo "Name: ". $name . " <br />";
echo "Email: ". $email . " <br />";
echo "Address: ". $address . " <br />";
echo "Home Phone: ". $homephone . " <br />";
echo "Alternate Phone: ". $alternatephone . " <br />";

echo "Activities: <br /><br />";

[B]foreach($_POST['activities'] as $selected_value ) {
 echo $selected_value."<br />";
      //do something
      }[/B]
echo "<br />"; 

echo "Special Events: <br /><br />";

[B]foreach($_POST['specialevents'] as $specialevents ) {
 echo $specialevents."<br />";
      //do something
      }[/B]
echo "<br />"; 


echo "Best Time Available: ". $best_time . " <br />";


$recipient = "email";
$subject   = "Volunteer";
$message   = " 


'. Name: .'$name 

'. Email: .' $email 

'. Address: .' $address 

'. Home Phone: .' $homephone 

'. Alternate Phone: .' $alternatephone 

'. Activites: .' [U]$selected_value[/U] 

'. Special Events: .' $specialevents 

'. Best Time: .' $best_time 

";

mail($recipient, $subject, $message);
?>

as you see the bold shows the selected events just fine but i need it to do so in the email too. and im a little confused about the underline part is everything suppose to be "selected_events"? because in the form i have the value as "activities". and did i miss understood what buddylee said? i just pasted the code on.

what do i need to name the table in the database? instead of activities to i have to rename it selected_value? and is that the same goes for the form? do i change it to selected value also? as of now when i use the $selected_value it doesnt show the selected values in the database. what type of table am i suppose to make? i just made a varchar is that okay?

Basically, its like this. You have a checkbox array in the form (eg. activities). Say, the user selects 3 options from the checkboxes. So, only those 3 checkboxes will be posted.

<?php
$some_variable = $_POST['activities'];
//this will assign checkbox array activities to $some_variable. If you try to insert $some_variable to a table, it will not insert the selected values but just "array" to the table. 
print $some_variable;
//prints 'array'

You have to iterate through the array and get them in the form of a string.
To do that, you can use buddylee17's code.

$selectedevents='';
foreach($_POST['activities'] as $val ) {
$selectedevents .= $val.", ";
}
$selectedevents=substr($selectedevents,0,-2);

By the end of the iteration, $selectedevents will have the selected values of the checkbox ($selectedevents will be a string and not an array). You can use $selectedevents to insert to the table or use it in mail (or whatever!).

yes ive done that part but i think maybe im not naming the table right? doesnt it have to have the same name and be in a particular order for it to post on the database?

so far i have this like you said but it is not posting in the database i put $selectedevents but it doesnt post but the table for the database is called activities. does that matter? and does the order in which i list them matter either?

i think maybe im not getting something for the bolded area of the code. ive done what you said so far im just worried im not naming tables fields in the database right and posting the $selectedevents in the right places.

<?php
$con = mysql_connect("host","user","pass"); //Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("volunteer", $con); 
$name=mysql_real_escape_string($_POST['name']); 
$email=mysql_real_escape_string($_POST['email']); 
$address=mysql_real_escape_string($_POST['address']); 
$homephone=mysql_real_escape_string($_POST['homephone']); 
$alternatephone=mysql_real_escape_string($_POST['alternatephone']); 
[B]$selectedevents=mysql_real_escape_string($_POST['activities']);[/B]
$specialevents=mysql_real_escape_string($_POST['specialevents']); 
$best_time=mysql_real_escape_string($_POST['best_time']); 
$sql="INSERT INTO volunteer (name,email,address,homephone,alternatephone,[B]activities[/B],specialevents,best_time) VALUES 

('$name','$email','$address','$homephone','$alternatephone','[B]$selectedevents[/B]','$specialevents','$best_time')"; /*form_data is the 

name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
 die('Error: ' . mysql_error());
}
?>

<?php
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$address = trim($_POST['address']);
$homephone = trim($_POST['homephone']);
$alternatephone = trim($_POST['alternatephone']);
[U]$activities = trim($_POST['activities']);[/U]
$specialevents = trim($_POST['specialevents']);
$best_time = trim($_POST['best_time']);


[B]$selectedevents='';
foreach($_POST['activities'] as $val ) {
$selectedevents .= $val.", ";
}
$selectedevents=substr($selectedevents,0,-2);[/B]


echo "Name: ". $name . " <br />";
echo "Email: ". $email . " <br />";
echo "Address: ". $address . " <br />";
echo "Home Phone: ". $homephone . " <br />";
echo "Alternate Phone: ". $alternatephone . " <br />";

echo "Activities: <br /><br />";

$selected_value='';
foreach($_POST['activities'] as $selected_value ) {
 echo $selected_value."<br />";
      //do something
      }
echo "<br />"; 

echo "Special Events: <br /><br />";

$specialevents='';
foreach($_POST['specialevents'] as $specialevents ) {
 echo $specialevents."<br />";
      //do something
      }
echo "<br />"; 


echo "Best Time Available: ". $best_time . " <br />";


$recipient = "email";
$subject   = "Volunteer";
$message   = " 


'. Name: .'$name 

'. Email: .' $email 

'. Address: .' $address 

'. Home Phone: .' $homephone 

'. Alternate Phone: .' $alternatephone 

'. Activites: .' $selectedevents 

'. Special Events: .' $specialevents 

'. Best Time: .' $best_time 

";

mail($recipient, $subject, $message);
?>

do i still need to keep this part of my original code after using yours :

$activities = trim($_POST['activities']);

What exactly are you trying to do ? In the above query, you are inserting the records into volunteer table. If you want to insert the values to activities table, change your query.. :-/

Edit:

$activities = trim($_POST);

This wouldn't work because $_POST is an array.

sorry that i seem to keep asking but i feel like im so close to getting this figured out i think the problem is my mysql database table here is a picture of what i made

[img=http://img440.imageshack.us/img440/5355/qahlft3.th.jpg]

i was told the order in which you put the $blah $blah2 has to be the order of the fields in the table? and also i meant that i still have that $selectedevents on the table as activities does that name of the field matter? or do i need to change it to match selectedevents? because that is the are where i need the checked boxes values to go.:S

i was told the order in which you put the $blah $blah2 has to be the order of the fields in the table?

Almost right. You can change the order in your insert query itself. That is,

$query = "insert into table (column1,column2) values ('$value1','$value2')";
or
$query = "insert into table (column2,column1) values ('$value2','$value1')";
$query = "insert into table (activities) values ('$xyz')";
$query = "insert into table (activities) values ('$activities')";
$query = "insert into table (activities) values ('$selectedevents')";

All the 3 queries above will work. So, you don't really have to change the column name.

This article has been dead for over six months. Start a new discussion instead.