Solved

email multiple checkbox values in PHP array

Posted on 2008-10-28
8
1,974 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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 109

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 250 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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 25 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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 75 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to count in a table in php 22 35
PHP and google maps 13 44
paypal ipn url 5 57
PHP Parse error: syntax error, unexpected '[' 1 24
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

773 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