skter4938 Posted July 5, 2006 Share Posted July 5, 2006 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=" Send " /> <input type="reset" value=" Reset " /> </form>[/code]could you please help out? Quote Link to comment https://forums.phpfreaks.com/topic/13795-contact-form-multiple-email/ Share on other sites More sharing options...
Travis Estill Posted July 5, 2006 Share Posted July 5, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/13795-contact-form-multiple-email/#findComment-53627 Share on other sites More sharing options...
skter4938 Posted July 5, 2006 Author Share Posted July 5, 2006 How do I assign that to $emailto? Quote Link to comment https://forums.phpfreaks.com/topic/13795-contact-form-multiple-email/#findComment-53628 Share on other sites More sharing options...
Travis Estill Posted July 5, 2006 Share Posted July 5, 2006 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". Quote Link to comment https://forums.phpfreaks.com/topic/13795-contact-form-multiple-email/#findComment-53630 Share on other sites More sharing options...
skter4938 Posted July 6, 2006 Author Share Posted July 6, 2006 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]<?phpsession_start();//Email Validationfunction 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 variablesswitch ($_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 messageif ($error_msg) {$_SESSION['error_msg'] = $error_msg;header ('Location: index.php');exit();}//sends emailelse {$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 pageif ($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] Quote Link to comment https://forums.phpfreaks.com/topic/13795-contact-form-multiple-email/#findComment-53637 Share on other sites More sharing options...
Travis Estill Posted July 6, 2006 Share Posted July 6, 2006 In the switch block you're using [b]$emailto[/b], but in the mail function you have [b]$EmailTo[/b] instead. Variable names are case-sensitive. Also, it's unnecessary to trim those POST variables more than once. Quote Link to comment https://forums.phpfreaks.com/topic/13795-contact-form-multiple-email/#findComment-53670 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.