0

I have a form that I am trying to get to send to different email addresses based on dropdown choice, and I know no php. I have pieced several form pieces together so I probably have conflicting data. I get it to act like it is working, after I hit submit, it brings me to my PHP page but the page shows blank and the email doesn't end up sending. I have tried to remove the extra unnecessary information, but I am at a loss as to what I am doing wrong.

HTML

<p><form name="htmlform" method="post" action="html_form_send-quote.php">
<table width="100%" border="0" cellspacing="10px" cellpadding="10px">
<tbody>
<tr>
  <td>Nearest Location*</td>
  <td><select id="mailTo" name"mailTo" onchange="changeVal()">
    <option value="loc1">Location 1</option>
    <option value="loc2">Location 2</option>
    <option value="loc3">Location 3</option>
    </select></td>
</tr>
<tr>
  <td>Name*</td>
  <td><input type="text" name="name" size="60" value=""></td>
</tr>
<tr>
  <td>E-Mail*</td>
  <td><input type="text" name="email" size="60" value=""></td>
</tr>
<tr>
  <td>I'm interested in* </td>
  <td><textarea name="comments" cols="60" rows="5"></textarea></td>
</tr>

My PHP

// CHANGE THE TWO LINES BELOW
switch($mailTo) {
    case 'loc1':
        $to = "email1@example.com";
        break;
    case 'loc2':
        $to = "email2@example.com";
        break;
    case 'loc3':
        $to = "email3@example.com";
        break;  
}
$result = mail($name, $email, $phone, $address, $city, $state, $zip, $calltime, $comments);

$email_subject = "Website Quote Request";


function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['phone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'Please enter your name<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'Please describe your requst.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
<html>...</html>

<?php
}
die();
?>
pfost74
  • 31
  • 7
  • Have you checked your error log? Read and apply this and see if you can get any error messages: http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – M. Eriksson Nov 29 '16 at 16:14
  • What does `changeVal()` on your select-box do? The values are `loc1, loc2, loc3` in the HTML, but the PHP is looking for `sd, en, wn`. Are you changing them at some point? And how do you populate `$mailTo`? – M. Eriksson Nov 29 '16 at 16:18
  • The code is full of errors. In your form `name"mailTo"` is missing the `=` sign. The [`mail`](http://php.net/manual/en/function.mail.php) function parameters are all wrong, you're sending 9 when it expects 5 and you're calling it twice, the second time suppressing errors `@mail`. Most of this could be solved by enabling error reporting. – mister martin Nov 29 '16 at 16:18

0 Answers0