using checkboxes in email forms

Hello! I hope someone can help me because this is getting frustrating. I’ve been searching and searching and finding a variety of answers to my dilema, unfortunately none work entirely.

Dilema: I have a simple php contact form that has worked properly until I added checkboxes. The results do not display in my email message. I understand that checkboxes need an array… but I am not experienced. I expect the php code looks horrible.

HTML Code for checkboxes:

 <input type="checkbox" name="Interests[]"  value="Wellness" /> <label for="wellness">Improving my wellness</label><br />
      <input type="checkbox" name="Interests[]" value="Saving"/> <label for="saving">Saving money</label><br />
      <input type="checkbox" name="Interests[]" value="Income" /> <label for="income">Earning extra income</label><br />
      <input type="checkbox" name="Interests[]" value="Helping" /> <label for="helping">Educating/helping others</label>

PHP Code

<?php // get posted data into local variables $EmailTo = [email][email protected][/email]; $Subject = "Form Submission"; $Name = Trim(stripslashes($_POST['Name'])); $Email = Trim(stripslashes($_POST['Email'])); $BestTimetobeReached = Trim(stripslashes($_POST['BestTimetoReached'])); $Phone = Trim(stripslashes($_POST['Phone'])); $CommentsorQuestions = Trim(stripslashes($_POST['CommentsorQuestions'])); $Referred = Trim(stripslashes($_POST['Referred'])); $Interests = Trim(stripslashes($_POST['Interests'])); foreach($_POST['Interests'] as $Interests) // validation $validationOK=true; if (Trim($Name)=="") $validationOK=false; if (Trim($Email)=="") $validationOK=false; if (!$validationOK) { print ""; exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Phone: "; $Body .= $Phone; $Body .= "\n"; $Body .= "Best Time to be Reached: "; $Body .= $BestTimetobeReached; $Body .= "\n"; $Body .= "Comments or Questions: "; $Body .= $CommentsorQuestions; $Body .= "\n"; $Body .= "Referred By: "; $Body .= $Referred; $Body .= "\n"; $Body .= $Interests; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){ print ""; } else{ print ""; } ?>


All help is greatly appreciated :slight_smile:

Replace these two lines of your code:
$Interests = Trim(stripslashes($_POST[‘Interests’]));
foreach($_POST[‘Interests’] as $Interests)

to this:
if(is_array($_POST[“Interests”])) $Interests=stripslashes(implode(",",$_POST[“Interests”]));

Sponsor our Newsletter | Privacy Policy | Terms of Service