calverdesigns Posted August 2, 2013 Share Posted August 2, 2013 Hi guys, I'm quite new to php - I've made forms before but not come across having to make checkbox options appear in email. I've searched the forum and have found similar posts but none of the answers have worked for me as I think it's the way I've written my code. What happens currently is when the form is emailed the "help" option just says Array. What I want it to do is give me multiple answers if the user has clicked more than one checkbox. - I think i'm close but just missing the php code and now I'm going round in circles. here is my html for the section I need to get working (checkboxes): <input type="checkbox" name="help" id="acting" value="Acting">Acting<br> <input type="checkbox" name="help[]" id="singing" value="Singing">Singing <br /> <input type="checkbox" name="help[]" id="dancing" value="Dancing">Dancing <br /> <input type="checkbox" name="help[]" id="general_advice" value="General Advice">General Advice And here is my PHP (in full) <?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "email@email.com"; $email_subject = "New"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form your submitted. "; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; die(); } // validation expected data exists if(!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['location']) || !isset($_POST['help']) || !isset($_POST['considering']) || !isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form your submitted.'); } $name = $_POST['name']; // required $email_from = $_POST['email']; // required $telephone = $_POST['telephone']; // required $location = $_POST['location']; // required $comments = $_POST['comments']; // required $help = $_POST['help']; // required $considering = $_POST['considering']; // required $error_message = ""; $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"; if(!eregi($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; } $string_exp = "^[a-z .'-]+$"; if(!eregi($string_exp,$name)) { $error_message .= 'The Name you entered does not appear to be valid.<br />'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = " ---------------------------------- FORM DETAILS BELOW ---------------------------------- \n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($name)." \n"; $email_message .= "Email: ".clean_string($email_from)." \n"; $email_message .= "Telephone: ".clean_string($telephone)." \n"; $email_message .= "Location: ".clean_string($location)." \n"; $email_message .= "I need help with: ".clean_string($help)." \n"; $email_message .= "I am considering: ".clean_string($considering)." \n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ;} ?> Any help would be greatly appreciated. Thanks Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 2, 2013 Share Posted August 2, 2013 The way the checkboxes are being named creates an array. To get the values out of the array and into the e-mail, you could use implode(): http://php.net/manual/en/function.implode.php Quote Link to comment Share on other sites More sharing options...
calverdesigns Posted August 2, 2013 Author Share Posted August 2, 2013 Hi, Thank you so much! It was implode I needed. I added this line to my php: $help .= implode(', ', $_POST['help']); Which now includes a "," between each selected option too! Thanks for the quick reply!! Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 2, 2013 Share Posted August 2, 2013 No problem. Note that the following checkbox isn't being added to the array: <input type="checkbox" name="help" id="acting" value="Acting">Acting<br> You need the square brackets after the name. 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.