Hi All,
I am a newbie to this site, as well as being a newbie with using scripting languages.
I've created a PHP contact form that when the submit button is clicked, I want it to send an e-mail to a certain address. Thanks so much for taking the time to look at this and help me! I'm so confused and frustrated!
I created the form in Dreamweaver. I first added code for server-side verification of the fields and then I used Dreamweaver's Spry Validation widgets to add client-side verification of the form elements.
In Firefox and Google Chrome, when I test the form by correctly filling out the different form elements and clicking the submit button:
- the form gets submitted
- the "thank you" message correctly displays
- and I receive an e-mail in my mailbox
The problem is with Internet Explorer 8. When I test the form by correctly completing the various form elements and clicking the submit button:
The form doesn't get submitted because
- I see the following statement "Sorry, there was a problem sending your message. Please try later." This statement displays because part of an "If-then-else" statement failed. It is part of the client-side verification code I created
- Since, the form wasn't submitted, I don't get the resulting e-mail.
I really don't know enough about PHP to debug. All I can figure is that for some reason IE thinks the
$_POST and
$mailSent variables are empty, so it displays the "sorry, there was a problem" error message.
Here's the code I'm using in the contact form page to display either error messages or a confirmation message:
<div id="formintro">
PHP Code:
if ($_POST && isset($missing) && !empty($missing)) {
<p class="warning2"><strong>Please complete the missing item(s) indicated.</strong></p>
PHP Code:
} elseif ($_POST && !$mailSent) {
?>
<p class="warning2">Sorry, there was a problem sending your message. Please try later.</p>
<?php
} elseif ($_POST && $mailSent) {
?>
<p class="acknowledgement"><strong>Thanks so much for taking the time to send us a note! We’ve received your message, and will contact you shortly!</strong></p>
} ?>
<p>Simply provide your information in the spaces provided below along with your comments or questions. You may also e-mail, call or write us directly with the provided information. <span style="color:#a91609; font-weight:bold;">Fields marked with an asterisk (*) are required and must be completed.</span>
</p>
</div>
The code to send the e-mail is in a separate include file. Here's the code I'm using to send the e-mail:
$mailSent = mail($to, $subject, $message, $headers);
if ($mailSent) {
// $missing is no longer needed if the email is sent, so unset it
unset($missing);
}
Here's a link to the form: http://www.buypuresilverbullion.com/.../contactus.php
Here's the code of the include file that contains the script to process the form:
Code:
<?php
if (isset($_SERVER['SCRIPT_NAME']) && strpos($_SERVER['SCRIPT_NAME'], '.inc.php')) exit;
// remove escape characters from POST array
if (PHP_VERSION < 6 && get_magic_quotes_gpc()) {
function stripslashes_deep($value) {
$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
}
// assume that there is nothing suspect
$suspect = false;
// create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';
// function to check for suspect phrases
function isSuspect($val, $pattern, &$suspect) {
// if the variable is an array, loop through each element
// and pass it recursively back to the same function
if (is_array($val)) {
foreach ($val as $item) {
isSuspect($item, $pattern, $suspect);
}
} else {
// if one of the suspect phrases is found, set Boolean to true
if (preg_match($pattern, $val)) {
$suspect = true;
}
}
}
// check the $_POST array and any subarrays for suspect content
isSuspect($_POST, $pattern, $suspect);
if ($suspect) {
$mailSent = false;
unset($missing);
} else {
// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
} elseif (in_array($key, $expected)) {
// otherwise, assign to a variable of the same name as $key
${$key} = $temp;
}
}
}
// validate the email address
if (!empty($email)) {
// regex to identify illegal characters in email address
$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
// reject the email address if it doesn't match
if (!preg_match($checkEmail, $email)) {
$suspect = true;
$mailSent = false;
unset($missing);
}
}
// go ahead only if not suspect and all required fields OK
if (!$suspect && empty($missing)) {
// initialize the $message variable
$message = '';
// loop through the $expected array
foreach($expected as $item) {
// assign the value of the current item to $val
if (isset(${$item}) && !empty(${$item})) {
$val = ${$item};
} else {
// if it has no value, assign 'Not selected'
$val = 'Not selected';
}
// if an array, expand as comma-separated string
if (is_array($val)) {
$val = implode(', ', $val);
}
// add label and value to the message body
$message .= ucfirst($item).": $val\n\n";
}
// limit line length to 70 characters
$message = wordwrap($message, 70);
// create Reply-To header
if (!empty($email)) {
$headers .= "\r\nReply-To: $email";
}
// send it
$mailSent = mail($to, $subject, $message, $headers);
if ($mailSent) {
// $missing is no longer needed if the email is sent, so unset it
unset($missing);
}
}
?>