Jump to content


Photo

Sending Checkbox Values to E-mail


  • Please log in to reply
4 replies to this topic

#1 XmnwildX12

XmnwildX12

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 12 February 2010 - 05:52 PM

I am brand new to using PHP, mostly only used an include statement. I found a script for a form to be sent to an email account from a website. I was able to add in more rows like address and etc. but I am not sure how to have checkbox values sent to the e-mail or drop down menu.

$name = array('Name:','name',NULL,NULL);
 $companyname = array('Company Name:','companyname',NULL,NULL);
 $position = array('Position/Title:','position',NULL,NULL);
 $address = array('Street Address:','address',NULL,NULL);
 $city = array('City:','city',NULL,NULL);
 $zip = array('Zip Code:','zip',NULL,NULL);
 $country = array('Country:','country',NULL,NULL);
 $phone = array('Phone #:','phone',NULL,NULL);
 $cell = array('Cellphone #:','cell',NULL,NULL);
 $email = array('E-mail:','email',NULL,NULL,NULL);
 $augers = array('Ice Augers:','augers',NULL,NULL);
 $subject = array('Subject:','subject',NULL,NULL);
 $message = array('Message:','message',NULL,NULL);
 $code = array('Code:','captcha_code',NULL,NULL,NULL);
 
 $error_message = '';
 
 if (!isset($_POST['submit'])) {
	 
 
   showForm();
 
 } else { //form submitted
 
   $error = 0;
   
   if(!empty($_POST['name'])) {
	   $name[2] = clean_var($_POST['name']);
	   if (function_exists('htmlspecialchars')) $name[2] = htmlspecialchars($name[2], ENT_QUOTES);
   }
   else {
	 $error = 1;
	 $name[3] = 'color:#FF0000;';
   }
   
   if(!empty($_POST['companyname'])) {
	   $companyname[2] = clean_var($_POST['companyname']);
	   if (function_exists('htmlspecialchars')) $companyname[2] = htmlspecialchars($companyname[2], ENT_QUOTES);
   }
   else {
	 $error = 1;
	 $companyname[3] = 'color:#FF0000;';
   }
   
   if(!empty($_POST['position'])) {
	   $position[2] = clean_var($_POST['position']);
	   if (function_exists('htmlspecialchars')) $position[2] = htmlspecialchars($position[2], ENT_QUOTES);
   }
   else {
	 $error = 1;
	 $position[3] = 'color:#FF0000;';
   }
   
   if(!empty($_POST['address'])) {
	   $address[2] = clean_var($_POST['address']);
	   if (function_exists('htmlspecialchars')) $address[2] = htmlspecialchars($address[2], ENT_QUOTES);
   }
   else {
	 $error = 1;
	 $address[3] = 'color:#FF0000;';
   }
   
   if(!empty($_POST['city'])) {
	   $city[2] = clean_var($_POST['city']);
	   if (function_exists('htmlspecialchars')) $city[2] = htmlspecialchars($city[2], ENT_QUOTES);
   }
   else {
	 $error = 1;
	 $city[3] = 'color:#FF0000;';
   }
   
   if(!empty($_POST['zip'])) {
	   $zip[2] = clean_var($_POST['zip']);
	   if (function_exists('htmlspecialchars')) $zip[2] = htmlspecialchars($zip[2], ENT_QUOTES);
   }
   else {
	 $error = 1;
	 $zip[3] = 'color:#FF0000;';
   }
   
   if(!empty($_POST['country'])) {
	   $country[2] = clean_var($_POST['country']);
	   if (function_exists('htmlspecialchars')) $country[2] = htmlspecialchars($country[2], ENT_QUOTES);
   }
   else {
	 $error = 1;
	 $country[3] = 'color:#FF0000;';
   }
   
   if(!empty($_POST['phone'])) {
	   $phone[2] = clean_var($_POST['phone']);
	   if (function_exists('htmlspecialchars')) $phone[2] = htmlspecialchars($phone[2], ENT_QUOTES);
   }
   else {
	 $error = 1;
	 $phone[3] = 'color:#FF0000;';
   }
   
   if(!empty($_POST['cell'])) {
	   $cell[2] = clean_var($_POST['cell']);
	   if (function_exists('htmlspecialchars')) $cell[2] = htmlspecialchars($cell[2], ENT_QUOTES);
   }
   else {
	 $error = 1;
	 $cell[3] = 'color:#FF0000;';
   }
   
   if(!empty($_POST['email'])) {
	   $email[2] = clean_var($_POST['email']);
	   if (!validEmail($email[2])) {
		 $error = 1;
		 $email[3] = 'color:#FF0000;';
		 $email[4] = '<strong><span style="color:#FF0000;">Invalid email</span></strong>';
	   }
   }
   else {
	 $error = 1;
	 $email[3] = 'color:#FF0000;';
   }
	
   if(!empty($_POST['subject'])) {
	   $subject[2] = clean_var($_POST['subject']);
	   if (function_exists('htmlspecialchars')) $subject[2] = htmlspecialchars($subject[2], ENT_QUOTES);	  
   }
   else {
	   $error = 1;
	 $subject[3] = 'color:#FF0000;';
   }  
 
   if(!empty($_POST['message'])) {
	   $message[2] = clean_var($_POST['message']);
	   if (function_exists('htmlspecialchars')) $message[2] = htmlspecialchars($message[2], ENT_QUOTES);
   }
   else {
	 $error = 1;
	 $message[3] = 'color:#FF0000;';
   }	
 
   if(empty($_POST['captcha_code'])) {
	 $error = 1;
	 $code[3] = 'color:#FF0000;';
   } else {
	   include_once "securimage.php";
		 $securimage = new Securimage();
	 $valid = $securimage->check($_POST['captcha_code']);
 
	 if(!$valid) {
	   $error = 1;
	   $code[3] = 'color:#FF0000;';   
	   $code[4] = '<strong><span style="color:#FF0000;">Incorrect code</span></strong>';
	 }
   }
 
   if ($error == 1) {
	 $error_message = '<span style="font-weight:bold;font-size:90%;">Please correct/enter field(s) in red.</span>';
 
	 showForm();
 
   } else {
	   
	   if (function_exists('htmlspecialchars_decode')) $name[2] = htmlspecialchars_decode($name[2], ENT_QUOTES);
	 if (function_exists('htmlspecialchars_decode')) $companyname[2] = htmlspecialchars_decode($companyname[2], ENT_QUOTES);
	 if (function_exists('htmlspecialchars_decode')) $position[2] = htmlspecialchars_decode($position[2], ENT_QUOTES);
	 if (function_exists('htmlspecialchars_decode')) $address[2] = htmlspecialchars_decode($address[2], ENT_QUOTES);
	 if (function_exists('htmlspecialchars_decode')) $city[2] = htmlspecialchars_decode($city[2], ENT_QUOTES);
	 if (function_exists('htmlspecialchars_decode')) $zip[2] = htmlspecialchars_decode($zip[2], ENT_QUOTES);
	 if (function_exists('htmlspecialchars_decode')) $country[2] = htmlspecialchars_decode($country[2], ENT_QUOTES);
	 if (function_exists('htmlspecialchars_decode')) $phone[2] = htmlspecialchars_decode($phone[2], ENT_QUOTES);
	 if (function_exists('htmlspecialchars_decode')) $cell[2] = htmlspecialchars_decode($cell[2], ENT_QUOTES);
	   if (function_exists('htmlspecialchars_decode')) $subject[2] = htmlspecialchars_decode($subject[2], ENT_QUOTES);
	   if (function_exists('htmlspecialchars_decode')) $message[2] = htmlspecialchars_decode($message[2], ENT_QUOTES);	  
	   
	 $message = "$name[0] $name[2]\r\n$companyname[0] $companyname[2]\r\n$position[0] $position[2]\r\n$address[0] $address[2]\r\n$city[0] $city[2]\r\n$zip[0] $zip[2]\r\n$country[0] $country[2]\r\n$phone[0] $phone[2]\r\n$cell[0] $cell[2]\r\n$email[0] $email[2]\r\n$augers[0] $augers[2]\r\n$message[0]\r\n$message[2]\r\n";
	 
	 if (!$from) $from_value = $email[2];
	 else $from_value = $from;
	 
	 $headers = "From: $from_value" . "\r\n" . "Reply-To: $email[2]";
	 
	 mail($to,"$subject_prefix - $subject[2]", $message, $headers);
	 
	 if (!$thank_you_url) {
	 
	   include $header_file;
	   echo $GLOBALS['thank_you_message'];
	   echo "\n";
	   include $footer_file;
	   }
	   else {
		   header("Location: $thank_you_url");
	   }
			
   }
 
 } //else submitted
 
 
 
 function showForm()
 
 {
 global $name, $companyname, $position, $address, $city, $zip, $country, $phone, $cell, $email, $subject, $augers, $message, $code, $header_file, $footer_file, $form_width, $form_background, $form_border_color, $form_border_width, $form_border_style, $cell_padding, $left_col_width, $font_size;	 
 include $header_file;
 echo $GLOBALS['error_message'];  
 echo <<<EOD
 
 <form method="post" class="cForm">
 <fieldset>
 <legend>Customer Information</legend>
 <ol>
 
 <li>
 <label style="{$name[3]}">{$name[0]}</label>
 <input type="text" name="{$name[1]}" value="{$name[2]}"  /></input>
 </li>
 
 <li>
 <label style="{$companyname[3]}">{$companyname[0]}</label>
 <input type="text" name="{$companyname[1]}" value="{$companyname[2]}"  /></input>
 </li>
 
 <li>
 <label style="{$position[3]}">{$position[0]}</label>
 <input type="text" name="{$position[1]}" value="{$position[2]}"  /></input>
 </li>
 
 <li>
 <label style="{$address[3]}">{$address[0]}</label>
 <input type="text" name="{$address[1]}" value="{$address[2]}" /></input>
 </li>
 
 <li>
 <label style="{$city[3]}">{$city[0]}</label>
 <input type="text" name="{$city[1]}" value="{$city[2]}"  /></input>
 </li>
 
 <li>
 <label style="{$zip[3]}">{$zip[0]}</label>
 <input type="text" name="{$zip[1]}" value="{$zip[2]}"  /></input>
 </li>
 
 <li>
 <label style="{$country[3]}">{$country[0]}</label>
 <input type="text" name="{$country[1]}" value="{$country[2]}"  /></input>
 </li>
 
 <li>
 <label style="{$phone[3]}">{$phone[0]}</label>
 <input type="text" name="{$phone[1]}" value="{$phone[2]}"  /></input>
 </li>
 
 <li>
 <label style="{$cell[3]}">{$cell[0]}</label>
 <input type="text" name="{$cell[1]}" value="{$cell[2]}"  /></input>
 </li>
 
 <li>
 <label style="{$email[3]}">{$email[0]}</label>
 <input type="text" name="{$email[1]}" value="{$email[2]}"  /> {$email[4]}</input>
 </li>
 
 <li>
 <label style="{$subject[3]}">{$subject[0]}</label>
 <input type="text" name="{$subject[1]}" value="{$subject[2]}"  /></input>
 </li>
 </ol>
 </fieldset>
 
 <fieldset>
 <legend>Ice Augers (Select All Products That You're Interested In)</legend>
 <ol>
	 <li>
 <label style="{$augers[3]}"></label>
					 <input type="checkbox"
					   name="augers[]"
					   value="EZ Hand Augers">
					   EZ Hand Augers<br>
					   
					 <input type="checkbox"
					   name="augers[]"
					   value="Mach II Power Augers">
					   Mach II Power Augers<br>
					   
					 <input type="checkbox"
					   name="augers[]"
					   value="Razr Electric Auger">
					   Razr Electric Auger<br>
					   
					 <input type="checkbox"
					   name="augers[]"
					   value="Razr Hand Augers">
					   Razr Hand Augers<br> 
					 
					 <input type="checkbox"
					   name="augers[]"
					   value="Razr Power Augers">
					   Razr Power Augers<br>
 </li>
 </ol>
 </fieldset>
 
 <fieldset>
 <legend>Feedback and Submit</legend>
 <ol>
 
 <li>
 <label style="block {$message[3]}">{$message[0]}</label>
 <textarea name="{$message[1]}" cols="40" rows="6">{$message[2]}</textarea>
 </li>
 
 <li>
 <label>&nbsp;</label>
 <img id="captcha" src="securimage_show.php" alt="CAPTCHA Image" />
 </li>
 
 <li>
 <label style="{$code[3]}">{$code[0]}</label>
 <input type="text" name="{$code[1]}" size="10" maxlength="5" /> {$code[4]}</input>
 </li>
 
 <p class="center">
 (Please enter the text in the image above. Text is not case sensitive.)<br />
 <a href="#" onclick="document.getElementById('captcha').src = 'securimage_show.php?' + Math.random(); return false">Click here if you cannot recognize the code.</a>
 </p>
 
 <p style="text-align:center; vertical-align:middle; padding:{$cell_padding}; font-size:90%; font-weight:bold;">
 All fields are required.
 </p>
 
 <p class="center">
 <input type="submit" name="submit" value="Submit" style="border:1px solid #999;background:#E4E4E4;margin-top:5px;" />
 </p>
 
 </fieldset>
 </form>
 
 EOD;
 
 include $footer_file;
 }
 
 function clean_var($variable) {
	 $variable = strip_tags(stripslashes(trim(rtrim($variable))));
   return $variable;
 }
 
 /**
 Email validation function. Thanks to http://www.linuxjournal.com/article/9585
 */
 function validEmail($email)
 {
	$isValid = true;
	$atIndex = strrpos($email, "@");
	if (is_bool($atIndex) && !$atIndex)
	{
	   $isValid = false;
	}
	else
	{
	   $domain = substr($email, $atIndex+1);
	   $local = substr($email, 0, $atIndex);
	   $localLen = strlen($local);
	   $domainLen = strlen($domain);
	   if ($localLen < 1 || $localLen > 64)
	   {
		  // local part length exceeded
		  $isValid = false;
	   }
	   else if ($domainLen < 1 || $domainLen > 255)
	   {
		  // domain part length exceeded
		  $isValid = false;
	   }
	   else if ($local[0] == '.' || $local[$localLen-1] == '.')
	   {
		  // local part starts or ends with '.'
		  $isValid = false;
	   }
	   else if (preg_match('/\\.\\./', $local))
	   {
		  // local part has two consecutive dots
		  $isValid = false;
	   }
	   else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
	   {
		  // character not valid in domain part
		  $isValid = false;
	   }
	   else if (preg_match('/\\.\\./', $domain))
	   {
		  // domain part has two consecutive dots
		  $isValid = false;
	   }
	   else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
	   {
		  // character not valid in local part unless 
		  // local part is quoted
		  if (!preg_match('/^"(\\\\"|[^"])+"$/',
			  str_replace("\\\\","",$local)))
		  {
			 $isValid = false;
		  }
	   }
	   if ($isValid && function_exists('checkdnsrr'))
	   {
		   if (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
		  // domain not found in DNS
		  $isValid = false;
		}
	   }
	}
	return $isValid;
 }
 
 
 ?>

Edited by XmnwildX12, 12 February 2010 - 11:07 PM.


#2 XmnwildX12

XmnwildX12

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 13 February 2010 - 12:30 AM

I have been trying since I posted this, so almost 8 straight hours and I havent gotten any closer..... :P

all I get is the word Array, where the check box items should be

Edited by XmnwildX12, 13 February 2010 - 12:46 AM.


#3 Wybe

Wybe

    Jedi In Training

  • Members
  • PipPip
  • 401 posts
  • Gender:Male
  • Location:the Netherlands
  • Interests:I have no interests

Posted 16 February 2010 - 04:02 AM

Your checkboxes are all named "augers[]", causing it to be an array. An array is really not 1 value, but a multiple ones. It's values are $_POST["augers"][0], $_POST["augers"][1], $_POST["augers"][2], $_POST["augers"][3], and so on. Depending on which checkboxes are selected, these values exist or do not. If you want to return/print/email an array, consider the following

$allthecheckboxes = implode(", ", $_POST["augers"]);

or

foreach($_POST["augers"] as $kkey=>$vvalue) {
echo $kkey.': '.$vvalue.'<br />';
}

or in case you just want to see the info

print_r($_POST["augers"]);

paste that in your php somewhere and see what it returns.

#4 whoopic

whoopic

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 01 August 2011 - 05:32 PM

My code is very similar, how can I add a drop down?

Thanks

#5 Demonslay

Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 973 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 21 August 2011 - 12:37 PM

Drop downs are accessed just like a regular input since they can only have on value.


...
<select name="my_select">
<option value="0">Item 0</option>
<option value="1">Item 1</option>
</select>
...



<?php
echo "You selected option {$_POST['my_select']}.";
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users