Multiple Select Problem

I have a from listed below that has a problem. I am wanting a web visitor to select multiple selections from the input “Interests” and then have those results emailed to me. Can someone tell me what I have done wrong and how I could fix it?

This should be easy for someone… just not me. Thanks in advance!
Todd

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Multiple Select</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php

/*	==============================================================================
	DEFINES DEFAULT FORM FIELD VALUES
	============================================================================== */
	if(!isset($_POST['name'])) { $_POST['name'] = ""; }
	if(!isset($_POST['interests'])) { $_POST['interests'] = ""; }
	if(!isset($_POST['message'])) { $_POST['yourMessage'] = ""; }
	//	EMAIL NOT SENT BY DEFAULT
	$emailSent = 0;

/*	==============================================================================
	IF FORM HAS BEEN SUBMITTED, CHECK FOR ERRORS, IF NO ERRORS, SEND EMAIL
	============================================================================== */
	if(!isset($formError)) { $formError = array(); }
	if (isset($_POST['submit'])) {

		//	SEND EMAIL IF NO ERRORS
		if(count($formError) == 0) {

			//	IF NO MESSAGE TYPED, SET TO "NO MESSAGE ENTERED"
			if (strlen(trim($_POST['message'])) == 0) {
				$_POST['yourMessage'] = "No Message Entered by Visitor";
			}

			//	CREATE EMAIL BODY
			$theBody = $theBody . "Name: " . $_POST['name'] . "\
";
			$theBody = $theBody . "Interests: " . $_POST['interests'] . "\
";
			$theBody = $theBody . "Sent: " . date('m-d-Y', time()) . " " . date('g:i A', time()) . "\
\
";

			//	DEFINE EMAIL TO SEND TO
			$emailTo = "test@domain.com";
			$emailSubject = "Feedback";

			//	SEND EMAIL
			mail($emailTo, $emailSubject, $theBody);

			$emailSent = 1;
		}

	}
?>
  
<?php if($emailSent) { ?>
  
<!-- SUBMIT CONFIRMATION -->
  
    Thank you.             
  
    <?php } else {

if(count($formError) == 0) { ?>
  
    <?php } else { echo "<ul>"; foreach($formError as $e) { echo "<li class=\\"error\\">$e</li>"; } echo "</ul><br />"; }?>

<form action="multiple_select.php" method="post">
<table width="523" border="0" align="left" cellpadding="4" cellspacing="0">
	<tr>
		<td width="200" align="right" valign="top">Name:</td>
		<td align="left" valign="middle"><input type="text" name="name" value="<?php echo $_POST['name'] ?>" size="24" tabindex="1"></td>
	</tr>
	<tr>
		<td align="right" valign="top">Interests:</td>
		<td align="left" valign="middle"><select name="interests[]" size="1" id="interests" tabindex="1" multiple="multiple">
          <option selected="selected">Please Select</option>
			<option value="Web Design"<?php if($_POST['interests'] == "Web Design") { echo " selected=\\"selected\\""; } ?>>Web Design </option>
			<option value="CSS"<?php if($_POST['interests'] == "CSS") { echo " selected=\\"selected\\""; } ?>>CSS </option>
			<option value="JavaScript"<?php if($_POST['interests'] == "JavaScript") { echo " selected=\\"selected\\""; } ?>>JavaScript </option>
			<option value="PHP"<?php if($_POST['interests'] == "PHP") { echo " selected=\\"selected\\""; } ?>>PHP </option>
			<option value="MySQL"<?php if($_POST['interests'] == "MySQL") { echo " selected=\\"selected\\""; } ?>>MySQL </option>
			<option value="jQuery"<?php if($_POST['interests'] == "jQuery") { echo " selected=\\"selected\\""; } ?>>jQuery </option>
        </select></td>
</tr>
<tr>
  <td align="right" valign="top">&nbsp;</td>
  <td align="left" valign="middle"><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
<?php } ?>
</body>
</html>

It is starting to work, but when I get the email of “Interests” they are all strung together. For example:

Web DesignCSSJavaScriptPHPjQuery

How can I add a comma and a extra space in there so that it appears like this:

Web Design, CSS, JavaScript, PHP, jQuery, etc.

Thank you both for your input!

Todd

hi Todd,
greenmedia is correct, I agree with him with what he pointed out.

in addition, I can’t seem to see where you actually check for errors. you might consider doing it like this


	function checkMultiple($multipleSelect){
		$stop = 0;
		$error = '';
		foreach($multipleSelect AS $x => $y){
			if(!$y && !is_array($y)){
				$stop = 1;
				$error = $x;
				break;
			}
			if(is_array($y)){
				for($i=0; $i<count($y); $i++){
					if(!$y[$i]){
						$stop = 1;
						$error = $x;
						break;
					}
				}
			}
		}
		
		return array($stop,$error);
	}

	$multipleSelect = array();
	$multipleSelect['name'] = isset($_POST['name']) ? $_POST['name'] : '';
	$multipleSelect['interest'] = isset($_POST['interests']) ? $_POST['interests'] : '';
	$multipleSelect['message'] = isset($_POST['yourMessage']) ? $_POST['yourMessage'] : '';
	
	if(isset($_POST['submit'])){
		$halt = checkMultiple($multipleSelect);
		if($halt[0]){
			echo 'error...' . $halt[1] . ' is required...';
			exit();
		}
		//continue here...
	}

set the default value of ‘interests’ to zero/empty otherwise it will return “Please Select” which is obviously not included on your “interests” options


<option value="0" selected="selected">Please Select</option>

i think you also forgot to include your textarea for the message


<textarea name="yourMessage" value=""></textarea>

I left user input filtering for you :slight_smile:

i took this from greenmedia’s code


$theBody = $theBody . $_POST['interests'][$x] . ", ";

just append the comma and space to $theBody

i do hope you did filter user inputs already :slight_smile:

You are outputting $_POST[‘interests’] as if it were a string but, because it is from a multi select it is actually an array.

You will need to loop through all the elements in that array to see them. For example


$theBody = $theBody . "Interests: ";
for($x=0;$x<count($_POST['interests']);$x++)
{
        $theBody = $theBody.$_POST['interests'][$x]." ";
}
$theBody = $theBody . "\
"; 

One more point. As the field is a mult select it might be an idea to set the size to show a few lines so that it is clear what is selected and what isn’t.