Ehailey Posted April 9, 2007 Share Posted April 9, 2007 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 Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 9, 2007 Share Posted April 9, 2007 this may help you $fields{"Type"} = "Type"; to <?php echo "SELCTED<BR>"; foreach ($_REQUEST['Type'] as $CB) { echo $CB; } ?> Quote Link to comment Share on other sites More sharing options...
Ehailey Posted April 9, 2007 Author Share Posted April 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 9, 2007 Share Posted April 9, 2007 OK first i can't see what you are trying to do with the check boxes, the loop 'should' of echo'd the value's sent from the form.. (just so you can see what values you recieved) what are you trying to use the checkboxes for ? Quote Link to comment Share on other sites More sharing options...
Ehailey Posted April 9, 2007 Author Share Posted April 9, 2007 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? Quote Link to comment Share on other sites More sharing options...
Ehailey Posted April 9, 2007 Author Share Posted April 9, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 9, 2007 Share Posted April 9, 2007 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; ?> Quote Link to comment Share on other sites More sharing options...
Ehailey Posted April 9, 2007 Author Share Posted April 9, 2007 Thank you, I think this is going to work! (If I can figure out why the code is now reporting the Ethnicity value as "Array"... Thank you so much for your help. Quote Link to comment Share on other sites More sharing options...
Ehailey Posted April 18, 2007 Author Share Posted April 18, 2007 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! Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 if you were still using your original code, you can see all the checked checkboxes by using: foreach($_POST['Type'] as $key => $val){ echo "{$val} was checked<br />\n"; } Quote Link to comment Share on other sites More sharing options...
Ehailey Posted April 19, 2007 Author Share Posted April 19, 2007 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? Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.