Forum Moderators: coopster
I have a separate e-mail form where the end user needs to select *where* the e-mail needs to go. The client wishes for this to be a required field, so if no option is selected from the dropdown box, he doesn't want the email to send, and an error message needs to display.
I have this working one way or the other (meaning: I can have the dropdown box switch to different e-mail addresses per user selection and it will send; or I can make it required to select an option from the dropdown box, and an error will display if no option is selected) - but it won't do both. If I try to make them function together, the error is displayed if nothing is chosen from the dropdown box, and a "Thank you" page appears - but the e-mail never sends. If I remove the required fields stuff - the email sends just fine.
In my PHP file, I have this to show the dropdown box stuff:
switch($mailto) {
case "sales":
$mailto = "email1@test.com";
break;
case "partners":
$mailto = "email2@test.com";
break;
case "support":
$mailto = "email3@test.com";
break;
case "other":
$mailto = "email4@test.com";
}
There is no "default" set, because if I set one, then the required thing doesn't work.
In the HTML document (called in from the PHP file) I have this:
<b <? if ($mailtoerror!= ""[smilestopper])?> >What is the nature of your inquiry?</b>
<select class="form" name="mailto">
<option value="" selected></option>
<option name="sales">Sales</option>
<option name="partners">Customer Support</option>
<option name="support">Partners</option>
<option name="other">Other</option>
</select>
Would anyone know, from looking at this, why the e-mails *won't* come through when I try to make the field required? It's as simple as removing "selected" from the blank option value to get the e-mail to come through - but if I don't have it there, then the "requiredness" of the field is gone.
Any help would be appreciated :[smilestopper])
On the HTML page:
if ($error == "1") {
echo "<span class='contact'><b>Please re-enter the entries noted below:";
echo "</b><br />We either had trouble understanding those fields, or need additional information.<ul>";}
if ($firstnameerror == "1")
{ echo "<li style='text-align:left; margin-left:20px'><span class='contact'>Please specify your first <b>name</b>.</span></li>"; }
if ($lastnameerror == "1")
{ echo "<li style='text-align:left; margin-left:20px'><span class='contact'>Please specify your <b>last name</b>.</span></li>"; }
if ($phoneerror == "1")
{ echo "<li style='text-align:left; margin-left:20px'><span class='contact'>Your <b>phone number</b> is invalid. Please check it and re-enter.</span></li>"; }
if ($emailerror == "1")
{ echo "<li style='text-align:left; margin-left:20px'><span class='contact'>Your <b>email</b> is invalid. Please check it and re-enter.</span></li>"; }
if ($mailtoerror == "1")
{ echo "<li style='text-align:left; margin-left:20px'><span class='contact'>You did not specify <b>the nature of your inquiry</b>. Please check and re-select.</span></li>"; }
if ($error == "1")
{ echo "</ul></span>"; }
The $mailto is the one I'm having trouble with. It's set up exactly like the others - and I've noticed that if I remove the "selected" from the blank option in the dropdown, then the error shows just fine, but the e-mail never comes through. If I remove it, the e-mail comes through, but the error messge doesn't show up.
I *did* have a default set, but when I had one, then no error message would show up.
The script in the PHP file is as such (I'll give you the whole thing):
switch($mailto) {
case "sales":
$mailto = "email1@test.com";
break;
case "partners":
$mailto = "email2@test.com";
break;
case "support":
$mailto = "email3@test.com";
break;
case "other":
$mailto = "email4@test.com";
break;
default:
//send nothing
break;
}
if($action!="sendmail"){
include ("contact.html");
exit;
}
if ($action == "sendmail") {
if ($firstname == "") {
$firstnameerror = "1";
$send = "no";
}
if ($lastname == "") {
$lastnameerror = "1";
$send = "no";
}
if (!ereg('(^(.*)[0-9]{3})(.*)([0-9]{3})(.*)([0-9]{4}$)', $phone)) {
$phoneerror = "1";
$send = "no";
}
if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{¦}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+$', $email) ¦¦ ereg("'", $email)) {
$emailerror = "1";
$send = "no";
}
if ($mailto == "") {
$mailtoerror = "1";
$send = "no";
}
if ($send == "no") {
$error = "1";
include ("contact.html");
exit;
}
$message = nl2br("Name: $firstname $lastname
Phone: $phone
e-Mail address: $email
$comments");
setcookie(cname, $name, time()+86400);
setcookie(cemail, $email, time()+86400);
setcookie(cmessage, $message, time()+86400);
$message = stripSlashes($message);
$mailheader = "From: $name <$email>\nContent-Type: text/html";
mail("N8 Systems <$mailto>","$subject","$message","$mailheader");
}
{
include("accept.html");
exit;
}
Any help would be appreciated :)
As stated before, I was looking ot have a dropdown box in my form that, when the user selected a dropdown option, that particular selection would send to an e-mail address that was supposed to go with it. This also had to be a required field in my form.
So, here's what I did:
in the HTML document, in the form, my dropdown looked like this:
<b <? if ($mailtoerror!= "")?> >Please select</b>
<select class="form" name="mailto">
<option value=""></option>
<option value="one" name="one">Option 1</option>
<option value="two" name="two">Option 2</option>
<option value="three" name="three">Option 3</option>
<option value="four" name="four">Option 4</option>
</select>
Then in the PHP code:
if($action!="sendmail"){
include ("contact.html"); //the html layout of the page
exit;
}
if ($action == "sendmail") {
//leaving out the other required fields stuff, as it's not important to my point here
$mailto = $_POST['mailto'];
switch ($mailto) {
case "":
$mailtoerror = "1";
$send = "no";
break;
case "one":
$emailto = "email@option1.com";
break;
case "two":
$emailto = "email@option2.com";
break;
case "three":
$emailto = "email@option3.com";
break;
case "four":
$emailto = "email@option4.com";
break;
}
if ($send == "no") {
$error = "1";
include ("contact.html");
exit;
}
$message = nl2br("Name: $firstname $lastname
Phone: $phone
e-Mail address: $email
$comments");
$message = stripSlashes($message);
$mailheader = "From: $name <$email>\nContent-Type: text/html";
mail("$emailto>","$subject","$message","$mailheader");
I was so happy when it worked! Hope this helps someone else so they don't have to go through my headache!