Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2011
  • Last Modified:

email multiple checkbox values in PHP array

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
razn8
Asked:
razn8
  • 2
  • 2
  • 2
  • +2
3 Solutions
 
Ray PaseurCommented:
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
 
MMDeveloperCommented:
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
 
cmaohioSenior Systems ManagerCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
razn8Author Commented:
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
 
cmaohioSenior Systems ManagerCommented:
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
 
razn8Author Commented:
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
 
Ray PaseurCommented:
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
 
ModernMattCommented:
Email Address information removed from Code Sample

ModernMatt
EE Moderator
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now