Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 107,708 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,095 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



checkboxes to email and database

 
Reply to this topicStart new topic

checkboxes to email and database

kiki13
post 11 Jul, 2008 - 09:40 PM
Post #1


New D.I.C Head

*
Joined: 11 Jul, 2008
Posts: 1

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:

CODE


<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:

CODE

<?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 & #40;name,email,address,homephone,alternatephone,activities,specialevents,best_ti
me) 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?

This post has been edited by kiki13: 11 Jul, 2008 - 09:42 PM
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 12 Jul, 2008 - 04:35 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,273



Thanked 72 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


When dealing with checkboxes, you have to remember that the value in PHP is going to come out as an array of values. So you can't simply use $_POST["activities"] to refer to all the choices. You can refer to the first value checked using $_POST["activities"][0] or you can loop through all their values like so...

CODE

<?PHP
    if (isset($_POST["submitcheckboxes"])) {
        // Lets display each of the values checked
        $checkboxes = $_POST["checkboxexamples"];
        
        // Loop through the array of checkboxexamples[]
        foreach ($checkboxes as $checkboxvalue) {
            echo "User chose option: $checkboxvalue<br/>";
        }
        echo "<br/>";
    }
?>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="checkbox" value="checkbox1value" name="checkboxexamples[]"/> Checkbox 1<br/>
<input type="checkbox" value="checkbox2value" name="checkboxexamples[]"/> Checkbox 2<br/>
<input type="checkbox" value="checkbox3value" name="checkboxexamples[]"/> Checkbox 3<br/>
<input type="submit" value="Submit" name="submitcheckboxes"/>
</form>


In the example above we loop through the checkboxexamples options. They came into PHP as an array of values. Each value represents one of the checkboxes that they checked in the form. If they checked no values, then this array will be non-existent. So be sure to check to make sure that there are values before you attempt to manipulate this array.

Hope this makes sense.

"At DIC we be checkbox array dealing code ninjas... we also deal in funkiness" decap.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/30/08 03:05AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month