Jump to content


Photo

contact form, multiple email


  • Please log in to reply
5 replies to this topic

#1 skter4938

skter4938
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 05 July 2006 - 10:39 PM

Hello there.

I have a contact form made, but I want the sender to choose to whome he wants to send it to, with a selection box. something like http://www.explosm.net/contact/

Here is the code in the contact.php part, where the form is processed.
// get posted data into local variables
$EmailTo = "skter4938@gmail.com"; 
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$Name = Trim(stripslashes($_POST['Name'])); 
$Subject = Trim(stripslashes($_POST['Subject'])); 
$Message = Trim(stripslashes($_POST['Message']));

And this is the accual form code
<form method="POST" action="contact.php">
				
				<select name="emailto">
    			<option value="artem" selected>Artem Artemov</option>
   			    <option value="kris">Kris Mobayeni</option>
  				</select>
								
				<p><label for="Name">Name</label><br />
				<input id="Name" name="Name" type="text" tabindex="1" size="30" /></p>
				
				<p><label for="EmailFrom">Email</label><br />
				<input id="EmailFrom" name="EmailFrom" type="text" tabindex="2" size="30" /></p>
				
				<p><label for="Subject">Subject</label><br />
				<input id="Subject" name="Subject" type="text" tabindex="3" size="30" /></p>
							
				<p><label for="words">Message</label><br />
				<textarea name="Message" tabindex="5" rows="10" cols="24" style="width:300px;"></textarea></p>
				
				<input type="submit" name="post" id="post" value="&nbsp;Send&nbsp;" />&nbsp;&nbsp;<input type="reset" value="&nbsp;Reset&nbsp;" />
			</form>

could you please help out?

#2 Travis Estill

Travis Estill
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 05 July 2006 - 11:15 PM

Use the switch construct:

switch ($_POST['emailto'])
   {
   case "artem":
      $emailto = "artem@gmail.com";
      break;
   case "kris":
      $emailto = "kris@gmail.com";
      break;
   default:
      die("Missing or invalid recipient");
      break;
   }

Travis
Freelance Web Developer with a focus on PHP/MySQL

Angel-Stones.com
ClanQ3.com

#3 skter4938

skter4938
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 05 July 2006 - 11:29 PM

How do I assign that to $emailto?

#4 Travis Estill

Travis Estill
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 05 July 2006 - 11:47 PM

That switch block will assign a certain value to $emailto depending on the value of $_POST['emailto']. Switch does basically the same thing as a series of If statements.

So, in the example above, if $_POST['emailto'] equals "artem", then $emailto gets set to "artem@gmail.com".
Freelance Web Developer with a focus on PHP/MySQL

Angel-Stones.com
ClanQ3.com

#5 skter4938

skter4938
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 06 July 2006 - 12:29 AM

It tells me that there is an error now.

Here is the full code to the contact.php. Can you look over it to see waht the error is?

<?php

session_start();
//Email Validation
function checkEmail($email) 
{
   if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 
   {
      return FALSE;
   }

   list($Username, $Domain) = split("@",$email);

   if(@getmxrr($Domain, $MXHost)) 
   {
      return TRUE;
   }
   else 
   {
      if(@fsockopen($Domain, 25, $errno, $errstr, 30)) 
      {
         return TRUE; 
      }
      else 
      {
         return FALSE; 
      }
   }
}


// get posted data into local variables
switch ($_POST['EmailTo']){
   case "artem":
      $emailto = "skter4938@gmail.com";
      break;
   case "kris":
      $emailto = "skter4938@hotmail.com";
      break;
} 
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$Name = Trim(stripslashes($_POST['Name'])); 
$Subject = Trim(stripslashes($_POST['Subject'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if(checkEmail($EmailFrom) == FALSE){
$error_msg[] = "Please enter in a valid email address.";
}
if (Trim($Name)==""){
$error_msg[] = "Please enter in a name.";
}
if (Trim($Subject)=="") {
$error_msg[] = "Please enter in a subject.";
}
if (Trim($Message)==""){
$error_msg[] = "Please enter in a message.";
}
//triggers error message
if ($error_msg) {
$_SESSION['error_msg'] = $error_msg;
header ('Location: index.php');
exit();
}
//sends email
else {
$Body = "You have a new email from $Name.\n\n Reply to $EmailFrom.\n\n $Name says,\n $Message";
$success = mail($EmailTo, $Subject, $Body);
}
//redirect to success page
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=../index.php\">";
}
else{
  print "Email could not be sent due to errors. Please email the <a href='mailto:skter4938@gmail.com>webmaster</a>.";
}
?>


#6 Travis Estill

Travis Estill
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 06 July 2006 - 03:49 AM

In the switch block you're using $emailto, but in the mail function you have $EmailTo instead. Variable names are case-sensitive. Also, it's unnecessary to trim those POST variables more than once.
Freelance Web Developer with a focus on PHP/MySQL

Angel-Stones.com
ClanQ3.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users