?
Solved

email multiple checkbox values in PHP array

Posted on 2008-10-28
8
Medium Priority
?
2,000 Views
Last Modified: 2012-05-05
I am trying to email a form with multiple checkbox values via PHP. I have a form page and a processor page. I have tried numerous methods from Google searches, but I always get an 'Array' value in the email. I believe I need to use a 'foreach' or 'if, else' statements, but everything I've tried results in either no value or an 'Array' value in the email. Does the array statement need to be on the form page or on the processor page? How does the array statement need to be written to work with my current code? Thanks!
Here's the form page (new-student-registration.php):
 
<div>
<form  id="form_new_student" method="post" action="new-student-submit.php">
<input name="new_student_date" type="hidden" value=" <?php  echo date("F d, Y"); ?> " />
<fieldset>
 
 
 
<div class="formdiv">
	<div >
  		<label for="new_student_name">Student's Name:</label>
	</div>
	<div >
    	<input name="new_student_name" type="text" class="required" id="new_student_name" title="Enter your name" size="50">
	</div>
</div>
 
 
<div  class="formdiv">
    <div>
      <label for=new_student_email>Email:</label>
    </div>
    <div> 
    	<input  name="new_student_email" type="text" class="required validate-email" id="new_student_email" title="Enter a valid email address" size="30">
    </div>
</div>
 
<div  class="formdiv">
	<div>
  		<label for="new_student_reasons">What are your main reasons for enrollment?</label>
  	</div>
    
	<div>
 
 
    <input class="inputcheckbox" id="new_student_reasons" type="checkbox" value="Self Defense" name="new_student_reasons[]">
     Self Defense &nbsp; &nbsp;
     
    
    <input class="inputcheckbox" id="new_student_reasons" type="checkbox" value="Self Esteem" name="new_student_reasons[]">
     Self Esteem &nbsp; &nbsp;
    
    
    <input class="inputcheckbox" id="new_student_reasons" type="checkbox" value="Coordination" name="new_student_reasons[]">
     Coordination &nbsp; &nbsp;
    
    
    <input class="inputcheckbox" id="new_student_reasons" type="checkbox" value="Recreation" name="new_student_reasons[]">
     Recreation&nbsp; 
     </div>
     
     <div>
     <input class="inputcheckbox" id="new_student_reasons" type="checkbox" value="Weight Control" name="new_student_reasons[]">
     Weight Control &nbsp; &nbsp;
     
    
    <input class="inputcheckbox" id="new_student_reasons" type="checkbox" value="Self Confidence" name="new_student_reasons[]">
     Self Confindence &nbsp; &nbsp;
     
    
    <input class="inputcheckbox" id="new_student_reasons" type="checkbox" value="Physical Conditioning" name="new_student_reasons[]">
     Physical Conditioning &nbsp;
     </div>
     
     <div>
    <input class="inputcheckbox" id="new_student_reasons" type="checkbox" value="Other" name="new_student_reasons[]">
     Other
     
    </div>
    
</div>
 
 
</fieldset> 
 
<div>
<input type="submit" name="submit" value="Submit Form" />
</div>
 </form>
 
 
<script type=text/javascript>
	function formCallback(result, form) {
	window.status = "valiation callback for form '" + form.id + "': result = " + result;
	}
						
	var valid = new Validation('form_new_student', {useTitles : true, immediate : true, onFormValidate : formCallback});
 
</script>
 
</div>
          
 
Here's the processor page (new-student-submit.php):
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
<head>
 
<title>Form Submitted</title>
 
</head>
 
<body> 
     
 
	<div id="form">
 
      
      
		 <?php
 
 
 
// Receiving variables
@$pfw_ip= $_SERVER['REMOTE_ADDR'];
@$new_student_date = addslashes($_POST['new_student_date']);
@$new_student_name = addslashes($_POST['new_student_name']);
@$new_student_email = addslashes($_POST['new_student_email']);
@$new_student_reasons = addslashes($_POST['new_student_reasons']);
 
 
 
 
// Validation
if (strlen($new_student_name) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter the student's name.</font></p>
<p align='center'><font face='Arial' size='3' color='#FF0000'>Click the Back button on your browser to return to the form page.</font></p>
");
}
 
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $new_student_email))
{
die("<p align='center'><font face='Arial' size='2' color='#000000'>Please enter your email.</font></p>
<p align='center'><font face='Arial' size='3' color='#FF0000'>Click the Back button on your browser to return to the form page.</font></p>
");
}
 
 
 
if (strlen($new_student_reasons) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please specify the student's reasons for enrollment.</font></p>
<p align='center'><font face='Arial' size='3' color='#FF0000'>Click the Back button on your browser to return to the form page.</font></p>
");
}
 
 
//Sending Email to form owner
$pfw_header = "From: Email Removed - ModernMatt 23 Jan 09 \n"
  . "Reply-To: $new_student_email\n";
$pfw_subject = "New Student Registration";
$pfw_email_to = "Email Removed - ModernMatt 23 Jan 09";
$pfw_message = "Visitor's IP: $pfw_ip\n"
. "date: $new_student_date\n"
. "name: $new_student_name\n"
. "email: $new_student_email\n"
. "reasons_for_training: $new_student_reasons\n";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
 
 
//Sending auto respond Email to visitor
$pfw_header = "From:  Removed - ModernMatt 23 Jan 09\n"
  . "Reply-To:  Removed - ModernMatt 23 Jan 09\n";
$pfw_subject = "New Student Registration";
$pfw_email_to = "$new_student_email";
$pfw_message = "Thank you for submitting your New Student Registration. \n"
. "\n"
. "Regards,\n"
. "New Student Registration";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
 
 
 
 
 
 echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>Success!</font></p>");
?>
 
 <blockquote>
   <h5 style="color: #54AF58"> Completed! Your new student information has been submitted.</h5>
 
      </blockquote>
 
	</div>
 
 
 
 
</body>
</html>

Open in new window

0
Comment
Question by:razn8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22823029
Add this statement to the top of your action script "new-student-submit.php"

var_dump($_POST);

Then post the output from that statement here.  We will talk you through it.  ~Ray
0
 
LVL 15

Accepted Solution

by:
MMDeveloper earned 1000 total points
ID: 22823113
The "reasons" are stored in a single-dimensional array so to print them in a CSV format..

$reasons = implode(", ", $_POST["new_student_reasons"]);

now $reasons will look like "Physical Conditioning, Self Confidence, Recreation"
0
 
LVL 5

Expert Comment

by:cmaohio
ID: 22823612
As MMDeveloper stated, the "new_student_reasons" variable is an array of values. So your loop would be like:

<?
foreach ($reason = $_POST["new_student_reasons"]) {
    ... //code to use $reason
}
?>

Open in new window

0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 

Author Comment

by:razn8
ID: 22824233
I added this to my // Receiving variables:

$new_student_reasons = implode(", ", $_POST["new_student_reasons"]);

and it successfully sent the values through email. This appears to solve my problem, or is there a better way to do this? What would I need the 'foreach' statement for? I have another form with two checkbox groups. Will this method works with that as well.
0
 
LVL 5

Assisted Solution

by:cmaohio
cmaohio earned 100 total points
ID: 22824345
The best way is probably the implode() way if you simply need a list of the reasons to be sent via e-mail.

The for each statement would be used if you needed to manipulate each individual reason.  Like:

$newreasons = array();
foreach ($reason = $_POST["new_student_reasons"]) {
 $newreasons[] = "This reason: $reason\n";
}

This simply change the reason to add "This reason" to the front and a breakline at the end.  That would be a reason to use the foreach. But again, if you're simply making a list, use MMDeveloper's method. That is the simplest.
0
 

Author Comment

by:razn8
ID: 22824378
Ray_Paseur: here is the var_dump($_POST); results you requested:

array(5) { ["new_student_date"]=> string(18) " October 28, 2008 " ["new_student_name"]=> string(4) "test" ["new_student_email"]=> string(18) "email@yahoo.com" ["new_student_reasons"]=> array(4) { [0]=> string(12) "Self Defense" [1]=> string(11) "Self Esteem" [2]=> string(12) "Coordination" [3]=> string(10) "Recreation" } ["submit"]=> string(11) "Submit Form" }
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 300 total points
ID: 22824477
Thanks.  For future reference, if you put echo "<pre>"; in front of var_dump,. it makes the output easier to read.

$_POST is an associative array with the form data in it.  One of the fields can be referred to this way:

$new_student_name = $_POST["new_student_name"];

The field you are interested in is $_POST["new_student_reasons"]

That field is also an associative array (right, an array within an array).

MMDeveloper's example with implode() that you used above will get you these reasons in a nice collection.  If you want to look at each individually, you can address them like this:

$my_reason = $_POST["new_student_reasons"][0];

IIRC, there is no input at all when a checkbox is not checked, so if $my_reason is empty, there were no checkmarks.  In another entry to the action script, there may also be multiple checkboxes checked.  You can find out how many with this:

$num_checkboxes = count($_POST["new_student_reasons"]);

On your other form with multiple checkbox groups you can follow this example, too.  It will work fine.

$group1 = array();
$group1 = $_POST["first_checkbox_collection"];

$group2 = array();
$group2 = $_POST["second_checkbox_collection"];

Best regards, ~Ray
0
 

Expert Comment

by:ModernMatt
ID: 23453461
Email Address information removed from Code Sample

ModernMatt
EE Moderator
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question