Emailing Values from a Checkbox

the mail() function

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Thu Feb 09, 2012 12:25 pm

Emailing Values from a Checkbox

Postby Sunstone » Fri Feb 10, 2012 2:01 am

Hi All

I know this topic has been done to death..but as someone who has very little knowledge of php..I've been asked to do a simple website with contact form that it required to be emailed. Here lies my problem.

I've looked across this forum and on other php pages on the net and have managed to gain a very basic understanding of php so far but just not enough to workout where I'm going wrong.

Basically when the form is completed and emailed I get all the correct info bar where Id expect the values to be for the checkboxes. I get the word 'Array'.

Can anyone point out where I 'm going wrong. I have managed to display the right into onto the webpage see code : echo($wastetype[$i] . " "); but I can't seem to get this info into an email ???

Heres my php :

Sendmail.php - send an email from a web form. Make sure this file is called sendmail.php
when you upload it, otherwise the example form won't find the script and will error.

NOTE: This script is heavily commented. Text after double slashes // is ignored by PHP

// You only need to modify the following three lines of code to customise your form to mail script.
$email_to = ""; // Specify the email address you want to send the mail to.
$email_subject = "Website Feedback - BUDGET SKIPS DIRECT"; // Set the subject of your email.
// Specify a page on your website to display a thankyou message when the mail is sent
$thankyou_url = "";

// Get the details the user entered into the form
$skip_size = htmlspecialchars ($_POST['skip_size']);
$postcode = htmlspecialchars ($_POST['postcode']);
$del_day = htmlspecialchars ($_POST['del_day']);
$del_month = htmlspecialchars ($_POST['del_month']);
$del_year = htmlspecialchars ($_POST['del_year']);
$col_day = htmlspecialchars ($_POST['col_day']);
$col_month = htmlspecialchars ($_POST['col_month']);
$col_year = htmlspecialchars ($_POST['col_year']);
$wastetype = $_POST['wastetype'];
echo("You didn't select any waste types.");
$N = count($wastetype);
echo("You selected $N waste types(s): ");
for($i=0; $i < $N; $i++)
echo($wastetype[$i] . " ");


$skiplocation = htmlspecialchars ($_POST['skiplocation']);
$name = htmlspecialchars ($_POST['name']);
$address = htmlspecialchars ($_POST['address']);
$email = htmlspecialchars ($_POST['email']);
$email_from = '';
$contact = htmlspecialchars ($_POST['contact']);

// Validate the email address entered by the user
//if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
// Invalid email address
//die("The email address entered is invalid.");

// The code below creates the email headers, so the email appears to be from the email address filled out in the previous form.
// NOTE: The \r\n is the code to use a new line.
$headers = "From: " . $email_from . "\r\n";
$headers .= "Reply-To: " . $email_from . "\r\n"; // (You can change the reply email address here if you want to.)

// Now we can construct the email body which will contain the name and message entered by the user
$message = "Name: ". $name . "\r\nAddress: " . $address ."\r\nPostcode: " . $postcode ."\r\nContact Number: " . $contact ."\r\nEmail: " . $email ."\r\nSkip Size: " . $skip_size ."\r\nDelivery Date: " . $del_day ." / " . $del_month ." / " . $del_year ."\r\nCollection Date: " . $col_day ." / ". $col_month ." / ". $col_year ."\r\nSkip Location: " . $skiplocation ."\r\nWaste Type: " . $wastetype;

// This is the important ini_set command which sets the sendmail_from address, without this the email won't send.
ini_set("sendmail_from", $email_from);

// Now we can send the mail we've constructed using the mail() function.
// NOTE: You must use the "-f" parameter on Fasthosts' system, without this the email won't send.
$sent = mail($email_to, $email_subject, $message, $headers, "-f" . $email_from);

// If the mail() function above successfully sent the mail, $sent will be true.
//if($sent) {
//header("Location: " . $thankyou_url); // Redirect customer to thankyou page
//} else {
// The mail didn't send, display an error.
//echo "There has been an error sending your message. Please try later.";


and heres the form (I've only included the checkbox section) :


<table class="skip_step5">
<td class="field_hdr">Paper</td><td><input type="checkbox" name="wastetype[]" value="paper" /></td>
<td class="field_hdr">Soil &amp clay</td><td><input type="checkbox" name="wastetype[]" value="soilandclay" /></td>
<td class="field_hdr">Hardcore</td><td><input type="checkbox" name="wastetype[]" value="hardcore" /></td>
<td class="field_hdr">Wood</td><td><input type="checkbox" name="wastetype[]" value="wood" /></td>
<td class="field_hdr">General Waste</td><td><input type="checkbox" name="wastetype[]" value="general" /></td>
<td class="field_hdr">Concrete</td><td><input type="checkbox" name="wastetype[]" value="concrete" /></td>
<td class="field_hdr">Metal</td><td><input type="checkbox" name="wastetype[]" value="metal" /></td>
<td class="field_hdr">Plastic</td><td><input type="checkbox" name="wastetype[]" value="plastic" /></td>
<td class="field_hdr">Green waste</td><td><input type="checkbox" name="wastetype[]" value="green" /></td>
<td class="field_hdr">Cardboard</td><td><input type="checkbox" name="wastetype[]" value="cardboard" /></td>
<br />
<br />


I have seen another post where some has the exact same issue..but as a complete newbie I can't easily read the code so thought it best to seek help.

I hope thats ok.

Many Thanks

Steve :help:

User avatar
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Emailing Values from a Checkbox

Postby egami » Fri Feb 10, 2012 6:54 am

Code: Select all

<form name="testme" method="post" action=".">
<input type="checkbox" name="sendmeemail" value="1"> Send Me E-Mail <br />
<input type="checkbox" name="sendmesnailmail" value="1"> Send Me Postal Mail <br />
<input type="submit" name="action" value="Submit">

Code: Select all

if (isset($_POST['action']) && $_POST['action'] == "Submit") { 
  if (isset(
$_POST['sendmeemail']) && $_POST['sendmeemail'] == 1) { 
"YAY! You want E-Mail! <br />\r\n";
  } else { 
"You chose not to receive E-Mail <br />\r\n";
  if (isset(
$_POST['sendmesnailmail']) && $_POST['sendmesnailmail'] == 1) { 
"Yay! You want to receive snail mail! <br />\r\n";
  } else { 
"You chose not to receive snail mail. <br />\r\n";

Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 2 guests