Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

emailing form

Status
Not open for further replies.

TrueJoker

Technical User
Jun 14, 2006
115
0
0
GB
I have created a form with checkboxes but I am stuck on how to display only the checked information in an email! i have tested the PHP document and it works I'm just not sure on how to send only the checked information! If anyone knows some example code regarding only sending checked information upon send i would appreciate it :)
 
browsers only submit checkbox values that have been checked.

if i have misunderstood what you mean, could you post some example code and indicate where you have problems?
 
Sorry my explanation was a bit vague lol but im having one of those days. the php code i am using is:

<?php
/*Here we are going to declare the variables*/
if (!empty($_POST['name'])) {
$name = stripslashes($_POST['name']);
} else {
$name = Null;
echo '<p><font color="red"><b>You did not enter your name!</b></font></p>';
}

if (!empty($_POST['email'])) {
$email = stripslashes($_POST['email']);
} else {
$email = Null;
echo '<p><font color="red"><b>You did not enter your email!</b></font</p>';
}

if (!empty($_POST['address'])) {
$address = stripslashes($_POST['address']);
} else {
$address = Null;
echo '<p><font color="red"><b>You did not enter an address!</b></font></p>';
}

if (!empty($_POST['herd'])) {
$herd = stripslashes($_POST['herd']);
} else {
$herd = Null;
echo '<p><font color="red"><b>You did not enter your herd size!</b></font></p>';
}

if (!empty($_POST['current'])) {
$current = stripslashes($_POST['current']);
} else {
$current = Null;
echo '<p><font color="red"><b>You did not enter your current parlor!</b></font></p>';
}

if (!empty($_POST['dealer'])) {
$dealer = stripslashes($_POST['dealer']);
} else {
$dealer = Null;
echo '<p><font color="red"><b>You did not specify your prefered local dealer!</b></font></p>';
}

$monza = stripslashes($_GET['monza']);
$daytona = stripslashes($_POST['daytona']);
$rapid = stripslashes($_POST['rapid']);
$swing = stripslashes($_POST['swing']);
$step = stripslashes($_POST['step']);
$flow = stripslashes($_POST['flow']);
$magnum = stripslashes($_POST['magnum']);
$phone = stripslashes($_POST['telephone']);
$fax = stripslashes($_POST['fax']);
$mobile = stripslashes($_POST['mobile']);
$newparlor = stripslashes($_POST['new']);
$select = stripslashes($_POST['select']);
?>
<?php
// if everything is okay then send.
if ($name && $address && $email && $herd && $current && $dealer) {
$date = date("l, F j, Y");
//Save visitor name and entered message into one variable:
$formcontent="$date\n\n$monza\n\n$daytona\n\n$rapid\n\n$swing\n\n$step\n\n$flow\n\n$magnum\n\nName: $name\n\nAddress: $address\n\nTelephone Number: $phone\n\nFax Number: $fax\n\nMobile: $mobile\n\nHerd Size: $herd\n\nCurrent Parlor: $current\n\nNew Parlor Required: $newparlor\n\nCurrently Using BGM Products: $select\n\nPrefered Local Dealer: $dealer";
$recipient = "me@here.com";
$subject = "Email Form\r\n";
$mailheader = "From: $email\r\n";
$mailheader .= "Cc: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
mail($recipient, $subject, $formcontent, $mailheader);

echo '<p><b>Form submitted successfully</b></p>';
} else {
echo '<p><font color="red"><b>Please got back and fill in the necessary field(s).</b></font></p>';
}
?>

what i am trying to achieve is post the name, address etc as well as a brochure that they have selected using a check box that they wish to be sent to them if that makes it any clearer :S
 
I had a few minutes so have restructured your code a bit (!).

basically you test for checkboxes like this
Code:
$checkboxvalue = 
             isset($_POST['checkbox'])
             ?  "value if set"
             : "value if not set";


i have not tested this code as i did not build a form to mirror yours. hopefully you will see that with minimum effort you can use the array that has been build to render the form as well. you would need to provide the values for the select box only.

Code:
<?php
$formvars = array(
	array ("name", "text", "empty", "name"),
	array ("email","text", "empty", "email"),	
	array ("address", "text", "empty", "address"),	
	array ("herd", "text", "empty", "herd size"),
	array ("current", "text", "empty", "current parlor", "Current Parlor"),	
	array ("dealer", "text", "empty", "preferred local dealer", "Preferred Local Dealer"),
	array ("monza", "checkbox"),
	array ("daytona", "checkbox"),
	array ("rapid", "checkbox"),
	array ("swing", "checkbox"),
	array ("step", "checkbox"),
	array ("flow", "checkbox"),
	array ("magnum", "checkbox"),
	array ("telephone", "text", null, null, "phone"),
	array ("fax", "text"),
	array ("mobile", "text"),
	array ("new", "text", null,null,"newparlor"),
	array ("select", "text", null, null, "Currently Using BGM Products"));
$errormessage = "";

if (!validate("POST")):
	die ($errormessage);
else:
	$cleansed = cleansedata("POST");
	$formcontent = buildFormContent();
	$recipient = "me@here.com";
	$subject = "Email Form\r\n";
	$mailheader = "From: $email\r\n";
	$mailheader .= "Cc: $email\r\n";
	$mailheader .= "Reply-To: $email\r\n";
	$mailheader .= "MIME-Version: 1.0\r\n";
	$result = mail($recipient, $subject, $formcontent, $mailheader);
	if ($result):	
	    echo '<p><b>Form submitted successfully</b></p>';
	else:
		echo "problem sending mail";
	endif;
endif;


function validate($type="POST") {
	global $formvars, $errormessage;
	$validate = true;
	foreach ($formvars as $formvar):
		if (isset($formvar[2])):
			switch ($formvar[2]):
				case "empty":
				
					if (empty($_{$type}[$formvar[0]])):
						$errormessage .= "<span style=\"color:red;\">You did not provide your ". $formvar[3] . "!</span><br/>";
						$validate=false;
					endif;
				break;
			endswitch;
		endif;
	endforeach;
	return $validate;
}

function cleansedata($type="POST"){
	global $formvars;
	foreach ($formvars as $formvar):
		if (magic_quotes_gpc()):
			if(isset($_{$type}[$formvar[0]])):
					$cleansed[$formvar[0]] = stripslashes($_{$type}[$formvar[0]]);
				else:
					$cleansed[$formvar[0]] = "No Data Provided";
			endif;		
		endif;
	endforeach;
	return $cleansed;
}
function doCheckBoxes() {
	global $formvars, $cleansed;
	foreach ($formvars as $formvar):
		if ($formvar[1] === "checkbox"):
			if ($cleansed[$formvar[0]] === "No Data Provided"):
				$cleansed[$formvar[0]] = "User has not selected " .$formvar[0];
			else:
				$cleansed[$formvar[0]] = "User has selected " .$formvar[0];
			endif;
		endif;
	endforeach;
}
function buildFormContent() {
	global $formvars, $cleansed;
	$date = date("l, F j, Y");
	$content = $date ."\r\n";
	foreach ($formvars as $formvar):
		$label = isset($formvar[4]) ? $formvar[4] : $formvar[0];
		$content .= <<<EOL
$label:			{$cleansed[$formvar[0]]}

EOL;
	endforeach;
}
?>

If you think this kind of thing is useful i would urge you to take a good look at quickform in pear.php.net.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top