Page 1 of 1

Making a string from an array

Posted: Wed Dec 01, 2004 7:35 pm
by Wldrumstcs
Ok, I have a mutliple-select form where the user selects email addresses, and then it sends an email to all the addresses selected. How would I make the email send to all the selected users because it doesn't the way it is. Here is my code:

Code: Select all

<?
$error = "";

mysql_connect("localhost","$username","$password") or die ("Unable to connect to MySQL server."); 
$db = mysql_select_db("$database") or die ("Unable to select requested database.");

$number = mysql_query("select count(*) from test");
$numberpost = mysql_result($number, 0);
$numberpost1 = "$numberpost+4";
$email = mysql_query("SELECT email FROM teachers WHERE id='$_COOKIE[id]'");

      while($row = mysql_fetch_array($email)) 
      { 
        $email_result = $row['email']; 
}

mysql_connect("localhost","$username","$password") or die ("Unable to connect to MySQL server."); 
$db = mysql_select_db("$database") or die ("Unable to select requested database.");

IF($_POST[submit]){
IF($_POST[subject] == "" OR $_POST[emailbody] == ""){
$error = "<font color='#FFFFFF'><b>Sorry, but one of the forms was left blank.</b></font>";
}ELSEIF($_POST[subject] != "" AND $_POST[emailbody] != ""){
$error = "<font color='#FFFFFF'><b>Sent, $_POST[filter_teachers]</b></font>";
IF($_POST[filter_teachers] == "showall"){
$result = mysql_query("SELECT email FROM test") or die("Invalid query: " . mysql_error());
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { 
if(mail($line[email], $_POST[subject], $_POST[emailbody], "From: $email_result")){
}}}ELSEIF($_POST[filter_teachers] != "showall"){
mail($_POST[filter_teachers], $_POST[subject], $_POST[emailbody], "From: $email_result");

}}}



?>
Here is the HTML/PHP that has the form:

Code: Select all

&lt;select multiple="multiple" size="6" name="filter_teachers&#1111;]"&gt;
  &lt;option value="showall"&gt;Choose a Teacher&lt;/option&gt;
  	&lt;option value="showall"&gt;----------------------&lt;/option&gt;
&lt;option value="showall"&gt;Send to All Teachers&lt;/option&gt;
&lt;option value="showall"&gt;----------------------&lt;/option&gt;
  	&lt;?

mysql_connect("localhost","$username","$password") or die ("Unable to connect to MySQL server."); 
$db = mysql_select_db("$database") or die ("Unable to select requested database.");

$query="SELECT * FROM test ORDER BY id ASC";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i &lt; $num) &#123;
$teachers=mysql_result($result,$i,"id");
$teachersemail=mysql_result($result,$i,"email");


	echo "
		  &lt;option $selected value='$teachersemail'&gt;$teachers&lt;/option&gt;
			";
			$i++;
&#125;

?&gt;	
 &lt;/select&gt;

Posted: Wed Dec 01, 2004 8:28 pm
by rehfeld

Code: Select all

$emails = array(); // fill this array w/ all the emails you want to send an email to

$to = implode(', ', $emails);

mail($to, $subject, $message);