Search code examples
phpemailformsmultiple-select

PHP email form multiple select


I'm trying to set up a simple PHP contact form for a website and I need some help modifying the PHP to list multiple items from a select menu and would appreciate the help. I'm a graphic designer, not a developer, so a lot of this is way over my head. This is the problem area here:

    <label for="Events[]">Which Event(s) Will You Be Attending?</label>
   <div class="input-bg">
              <select name="Events[]" size="6" multiple="MULTIPLE" class="required" id="Events[]">
                <option value="Wednesday">Portfolio Show June 16</option>
                <option value="Thursday">Portfolio Show June 17</option>
                <option value="Saturday">Graduation Ceremony</option>
                <option value="Saturday Eve">Graduation Party</option>
                <option value="Not Sure">Not Sure</option>
                <option value="Not Coming">Not Coming</option>
              </select>
      </div>

And here's the PHP:

    <?php

// CHANGE THE VARIABLES BELOW

$EmailFrom = "[email protected]";
$EmailTo = "[email protected]";
$Subject = "Graduation RSVP";

$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Guests = Trim(stripslashes($_POST['Guests'])); 
$Events = Trim(stripslashes($_POST['Events'])); 

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Guests: ";
$Body .= $Guests;
$Body .= "\n";
$Body .= "Events: ";
$Body .= $Events;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
// CHANGE THE URL BELOW TO YOUR "THANK YOU" PAGE
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=http://justgooddesign.net/graduation\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=http://justgooddesign.net/graduation/error.html\">";
}
?>

Any help really is appreciated!


Solution

  • $_POST['Events'] will be an array. You could use the implode() function to join them in a comma separated string:

    $Events = Trim(stripslashes(implode(",", $_POST['Events'])));
    

    Alternatively, you could iterate over them individually using a foreach statement.