Jump to content

Checkboxes in a multidimensional Array (I think?)


Ehailey

Recommended Posts

Hello,

 

I found this nice code online that does exactly what I want it do to: Emails a form that includes the posted values. I can get the text boxes, text areas, and radio buttons to send; however I cannot for the life of me figure out how to get checkboxes to send. It will only send the last-checked value.

 

I have searched the internet and seen that this is a common problem, but I have not found anything similar to the code that I am using.

 

Here is the checkbox part of the form:

 

 

 

<input size=25 name="Name">

<input type="checkbox" name="Type[]" value="Type1">

<input type="checkbox" name="Type[]" value="Type2">

<input type="checkbox" name="Type[]" value="Type3">

 

 

 

Here is the processing part of the form:

 

 

 

<?php

$to = "email@email.com" ;

$from = $_REQUEST['Email'] ;

$name = $_REQUEST['Name'] ;

$headers = "From: $from";

$subject = "Results";

 

$fields = array();

 

$fields{"Name"} = "Name";

$fields{"Type"} = "Type";

 

 

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

 

$headers2 = "From:(E-Mail address blocked: See forum rules)";

$subject2 = "Thank You for Your Application!";

$autoreply = "Thank you!" ;

 

 

 

if($from == '') {print "You have not entered an email, please go back and try again";}

else {

if($name == '') {print "You have not entered a name, please go back and try again";}

else {

$send = mail($to, $subject, $body, $headers);

$send2 = mail($from, $subject2, $autoreply, $headers2);

if($send)

{header( "Location: thankyou.html" );}

else

{print "We encountered an error sending your mail, please notify(E-Mail address blocked: See forum rules)"; }

}

}

?>

 

 

 

I get the name value posted perfectly in my email. I can see that the foreach command in the later part of the code loops through the field array and collects all of the data. What do I have to do to this code to get the "type" to post as an array and show up under the field variable "type"??

 

I hope this makes sense... I am still a beginner at php.

 

Thank you!

 

Sarah

 

Link to comment
Share on other sites

Hi, thank you for your response! That didn't seem to help... And I am pretty sure that I do not want to echo anything onto the contact form. From what I can tell, it seems that the foreach command in the code tells the script to assign the data in a string format (sprintf?) to the $body variable of the email. The $field array has several different variables, and I am wondering if I can make a single $field="Type" variable contain the checkbox array.

Link to comment
Share on other sites

Here is a copy of the form on my website:

 

http://aughraseye.no-ip.com/apptest.html

 

I want people to be able to select multiple ethnicities when they fill out the form, and when it is sent to email, I want each ethnicity to show up in one line of text. So far, everything shows up in an email like this:

 

Name: John Doe

Email: Email@email.com

Age: 24

Sex: Male

Feet: 5

Inches: 4

Weight: 475

Ethnicity: Asian/Pacific Islander

 

I want the ethnicity to show up as:

 

Ethnicity: African American/Black, Asian/Pacific Islander

 

 

 

 

Does that make sense?

Link to comment
Share on other sites

erm.. change

 

<?php
$fields{"Type"} = "Type"; 

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

?>

 

to

<?php

$Ethnicity = "";
foreach ($_REQUEST['Type'] as $CB)
{
$Ethnicity .= $CB.", ";
}

$body = "We have received the following information:\n\n";
foreach($fields as $a => $b)
{
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
$body .= $Ethnicity;
?>

 

 

Link to comment
Share on other sites

  • 2 weeks later...

Ok... I thank you so much for your help, but this piece of code is just a little beyond me. I should have just posted the original code to begin with, rather than trying to summarize and substitute it. When I tried what you said to do, it didn't show up in the email. So here is the code I'm using:

 

The form (minus visible text, etc):

 

<input size=25 name="Name">
<input size=25 name="Email">
<input type="radio" name="Sex" value="Male"> 
<input type="radio" name="Sex" value="Female">
<input type="checkbox" name="Ethnicity" value="African American/Black"> 
<input type="checkbox" name="Ethnicity" value="Asian/Pacific Islander"> 

 

 

The processing page:

 

<?php 
$to = "email@email.com" ; 
$from = $_REQUEST['Email'] ; 
$name = $_REQUEST['Name'] ; 
$headers = "From: $from"; 
$subject = "Thank You!"; 

$fields = array(); 
$fields{"Name"} = "Name"; 
$fields{"Email"} = "Email"; 
$fields{"Sex"} = "Sex"; 
$fields{"Ethnicity"} = "Ethnicity"; 


$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: 

%s\n",$b,$_REQUEST[$a]); } 


$headers2 = "From: email@email.com"; 
$subject2 = "Thank you for contacting us"; 
$autoreply = "Thank you for contacting us.";

if($from == '') {print "You have not entered an email, please go back and try again";} 
else { 
if($name == '') {print "You have not entered a name, please go back and try again";} 
else { 
$send = mail($to, $subject, $body, $headers); 
$send2 = mail($from, $subject2, $autoreply, $headers2); 
if($send) 
{header( "Location: thankyou.html" );} 
else 
{print "We encountered an error sending your mail, please notify email@email.com"; } 
}
}
?> 

 

 

everything works perfectly: I get all of these variables reported in my email... however I only get Asian/Pacific Islander reported for Ethnicity when I check both boxes.

 

 

Your help is very appreciated. Thank you!

Link to comment
Share on other sites

Actually, that's not true... I don't want the code to report the results on the php page, I want them to be sent in an email. Why would I want it to echo this when nothing else uses the echo command?

 

for debugging purposes. to teach you how to you help yourself.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.