views:

153

answers:

3

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\"&gt;";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=http://justgooddesign.net/graduation/error.html\"&gt;";
}
?>

Any help really is appreciated!

+1  A: 

$_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.

Andy E
Thank you, that helped a lot! Much of this stuff goes right over my head.
Justin
We all start out somewhere, just happy to help :-)
Andy E
+1  A: 

If you do not want to join the events by commas but want to use them in some other way, you could loop through the events for a foreach loop. Instead of

$Body .= $Events;

You could use:

foreach($Events as $event) {
    $Body .= '- $event\n';
}

That example would but each event on its own line with a ' -' in front of it, making it look like an bullet list.

Trey
+1 for a `foreach` example which, admittedly, I was too lazy to provide :-)
Andy E
+1  A: 

When the name of an element in your form ends with brackets, that means that PHP will put the values into an array. In other words, if the person chooses the first two options, the value of $_POST['Events'] will be the same as array('Wednesday', 'Thursday'). If you just want a string that contains all of the options (Something like "Wednesday Thursday"), you can use implode(). If you want to check for the presence of a specific item, you can use in_array(). For example:

if (in_array('Wednesday', $_POST['Events']))
{
    echo 'You selected Wednesday!';
}

As a side note, I would recommend rethinking select controls that allow multiple selections. Many people aren't used to this behavior and won't realize they can select more than one. Checkboxes are much clearer.

matthew