ernielou Posted June 7, 2009 Share Posted June 7, 2009 Hi, I am very new to posting on a forum like this, i am also very very new to learning php and am not a great programmer (i am still at college). However I have tried PHP and much prefer it to ASP much to the dismay (i have no idea why) of my lecturer, so asking him for help is a no go, which leads me here, I cannot bear having to admit to him i couldnt do it! I am trying to design a form that sends the results to an email. This works fine except for the checkboxes, i just need them to return something to imply they have been checked. I understand this seems to be a regular problem and have tried a few things from googling it, but i am not very good at amending code from another problem (i have found lots to do with databases) to fit my problem and i havent found anything specific enough. I have left in a few things i have tried, commented out, that havent worked, but these efforts may be met by howls of derision The code i am posting is test code for it, with 1 set of checkboxes, the actual form is huge (to me) and has 2 sets of checkboxes the second set being about 26 items, the whole form has about 56 items, so I have tried to cut down the code to just the 1 set, but if something looks a bit odd it may be where i have butchered the code. html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>VCACD</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link rel="stylesheet" type="text/css" href="../css/stylesheet.css" /> </head> <body> <form id="form1" method="POST" action="memberTest.php"><br/><p align="left"><label><strong>Organisation Name:</strong></label> <br /> <input name="organisation" type="text" id="organisation" size="60" /></p> <p align="left"> <label><strong>Email:</strong><br /> <input name="email" type="text" id="email" size="60" /> </label> </p> <input type="checkbox" name="CheckboxGroup3[]" value="Newsletter" /> Would like Quarterly Newsletter</label> <br /> <label> <input type="checkbox" name="CheckboxGroup3[]" value="email" /> Would like Email Bulletin</label> <br /> <label> <input type="checkbox" name="CheckboxGroup3[]" value="directory" /> Entered on VCACD Directory</label> <br /> <label> <input type="checkbox" name="CheckboxGroup3[]" value="mail" /> Other correspondance through Mail</label> </p> <input type="submit" name="Submit2" id="Submit2" value="Submit" /> </form> </div> </div> </div> </body> </html> <?php $to = "helenwake8@hotmail.com" ; $email = $_REQUEST['email'] ; $organisation = $_REQUEST['organisation'] ; $headers = "From: $email"; $subject = "Membership Request"; $fields = array(); $fields{"organisation"} = "Organisation"; $fields{"email"} = "Email"; $fields{"CheckboxGroup3"} = "Quarterly Newsletter"; $fields{"CheckboxGroup3"} = "Email Bulletin"; $fields{"CheckboxGroup3"} = "VCACD Directory"; $fields{"CheckboxGroup3"} = "Mail"; //$fields{"radio"} = "test"; //if (isset($_POST['Submit2'])) { // $CheckboxGroup3 = $_POST["CheckboxGroup3"]; // $how_many = count($CheckboxGroup3); // echo 'Categories chosen: '.$how_many.'<br><br>'; // if ($how_many>0) { // echo 'You chose the following categories:<br>'; // } // for ($i=0; $i<$how_many; $i++) { // echo ($i+1) . '- ' . $CheckboxGroup3[$i] . '<br>'; // } // echo "<br><br>"; //} $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } //$CheckboxGroup3=$_POST['CheckboxGroup3']; //foreach ($CheckboxGroup3 as $CHECKED) //{ //echo "$CHECKED is checked"; //} $headers2 = "From: helenwake8@hotmail.com"; $subject2 = "Thank you for contacting us"; $autoreply = "Thank you for contacting us. We will add your organisation to our members. If you have any more questions, do not hesitiate to contact us"; $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if($send) {print " thank you for your email";} else {print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } ?> Thank you, any help would be much appreciated, Helen Quote Link to comment Share on other sites More sharing options...
phpdragon Posted June 7, 2009 Share Posted June 7, 2009 assign all the check boxes different names then simply refer to them as posted variables and check for them eg <input type="checkbox" name=newsletter" value="Newsletter" /> Would like Quarterly Newsletter</label> <br /> <label> <input type="checkbox" name="email" value="email" /> Would like Email Bulletin</label> <br /> <label> <input type="checkbox" name="directory" value="directory" /> Entered on VCACD Directory</label> <br /> <label> <input type="checkbox" name="mail" value="mail" /> then on your processor page if (isset($_POST['Newsletter'])) $newsletter="Quarterly Newsletter";[ if (isset($_POST['email'])) $email="Email Bulletin"; if (isset($_POST['directory'])) $directory="VCACD Directory"; if (isset($_POST['mail'])) $mail="Mail";/code] these variables are then available for your use in the email Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 7, 2009 Share Posted June 7, 2009 When you did $_POST['CheckboxGroup3'], you got an array of values of the selected checkboxes right? Well, then loop through them and process each one. I think you had that in your commented block. 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.