I have a php Mail Form that people will also fill in a different language from english.
It works fine, except one thing.
When the user fills in something invalid or leaves a required field empty and submits,
then the redirect page with an error message appears.
The text that the user wrote before submit inside the fields still exists,
but the language type is not in UTF-8, so the text is unreadable (except its english).
I had the same problem when a mail from this form was received was also unreadable so i added:
Code: Select all
$headers .= "Content-Type: text/plain; charset=utf-8";Please... if anyone knows the solution help me.
(sorry for my bad english)
The php code of this form is:
Code: Select all
<?php
if (array_key_exists('send', $_POST)) {
//mail processing script
// remove escape characters from POST array
if (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);
}
$to = 'unstanic@hotmail.com'; // use your own email address
$subject = 'Travel House Online Tickets';
// list expected fields
$expected = array('ekdromi', 'apo', 'ews', 'enilikes', 'paidia', 'monoklina', 'diklina', 'triklina', 'sxolia1', 'name', 'surname', 'telephone', 'sellphone', 'email', 'fax', 'city', 'adress', 'tk');
// set required fields
$required = array('ekdromi', 'apo', 'ews', 'enilikes', 'paidia', 'monoklina', 'diklina', 'triklina', 'name', 'surname', 'telephone', 'city', 'adress', 'tk');
// create empty array for any missing fields
$missing = array();
// 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);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$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 deosn't match
if (!preg_match($checkEmail, $email)) {
$suspect = true;
$mailSent = false;
unset($missing);
}
}
// go ahead only if not suspsect and all required fields OK
if (!$suspect && empty($missing)) {
// build the message
$message = "?????: $name\n";
$message .= "???????: $surname\n";
$message .= "????????: $telephone\n";
$message .= "??????: $shellphone\n";
$message .= "Email: $email\n";
$message .= "Fax: $fax\n\n";
$message .= "???????: $city\n";
$message .= "?????????: $adress\n";
$message .= "?.?.: $tk\n\n\n";
$message .= "???????: $ekdromi\n";
$message .= "???: $apo\n";
$message .= "???: $ews\n";
$message .= "????????: $enilikes\n";
$message .= "??????: $paidia\n";
$message .= "?????????: $monoklina\n";
$message .= "???????: $diklina\n";
$message .= "????????: $triklina\n\n\n";
$message .= "$sxolia1";
// limit line length to 70 characters
$message = wordwrap($message, 70);
// create additional headers
$headers = "From: Travel House<travelhouse@website.com>\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8";
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);
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
...
<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
<p class="warning">???????????, ??????????? ?? ???? ?????.</p>
<?php
}
elseif ($_POST && !$mailSent) {
?>
<p class="warning">???????, ????????????? ???????? ???? ????????. ??????????? ???????????? ???? ????????.</p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p class="sentc"><strong>?? ?????? ??? ??????. ???????????? ??? ??? ??????? ???. ??????? ?? ??????????????? ???? ???.
</strong></p>
<?php } ?>
...
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form1" id="form1">
...
<div class="chkRad">
<p>
<label for="name"><font color="#fa7409">*</font> ?????<?php
if (isset($missing) && in_array('name', $missing)) { ?>
<span id="regular" class="warning"> !</span><?php } ?></label>
<input type="text" name="name" class="textinput3" id="name"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['name']).'"';
} ?>
/>
</p>
<p>
<label for="surname"><font color="#fa7409">*</font> ???????<?php
if (isset($missing) && in_array('surname', $missing)) { ?>
<span id="regular" class="warning"> !</span><?php } ?></label>
<input type="text" name="surname" class="textinput3" id="surname"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['surname']).'"';
} ?>
/>
...
<p class="clearIt">
<input type="submit" name="send" id="send" value="????????" />
</p></form>