Jump to content

Recommended Posts

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.
[code]// 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'])); [/code]

And this is the accual form code
[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>[/code]

could you please help out?
Link to comment
https://forums.phpfreaks.com/topic/13795-contact-form-multiple-email/
Share on other sites

Use the switch construct:

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

Travis
That [url=http://us2.php.net/manual/en/control-structures.switch.php]switch[/url] 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".
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?

[code]<?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>.";
}
?>[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.