[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Помогите с обработчиком формы обратной связи.
alice70
Здравствуйте!
В PHP-я полный баран, поэтому для формы обратной связи воспользовался сервисом генератора форм.
В результате получил то что на почту в полях ИМЯ и СООБЩЕНИЕ приходят кракозябры.
Помогите пожалуйста-самому мне не разобраться.
Код формы:


<html>
<head>
<meta charset="windows-1251">
<title></title>
<link href="css/style.css" media="screen" rel="stylesheet" type="text/css"/>
<link href="css/uniform.css" media="screen" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.tools.js"></script>
<script type="text/javascript" src="js/jquery.uniform.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>

<div class="TTWForm-container">


<div id="form-title" class="form-title field">
<h2>
Запись на процедуры.
</h2>
</div>


<form action="process_form.php" class="TTWForm" method="post" novalidate="">


<div id="field1-container" class="field f_100">
<label for="field1">
Ваше имя:
</label>
<input name="field1" id="field1" required="required" type="text">
</div>


<div id="field2-container" class="field f_100">
<label for="field2">
Ваш телефон:
</label>
<input name="field2" id="field2" required="required" type="text">
</div>


<div id="field3-container" class="field f_100">
<label for="field3">
Название процедуры:
</label>
<input name="field3" id="field3" required="required" type="text">
</div>


<div id="field5-container" class="field f_100">
<label for="field5">
Дата визита:
</label>
<input name="field5" id="field5" required="required" type="number">
</div>


<div id="field6-container" class="field f_100">
<label for="field6">
Время визита:
</label>
<input name="field6" id="field6" required="required" type="number">
</div>


<div id="field7-container" class="field f_100">
<label for="field7">
Ваше сообщение:
</label>
<textarea rows="5" cols="20" name="field7" id="field7" required="required"></textarea>
</div>


<div id="form-submit" class="field f_100 clearfix submit">
<input value="Записаться" type="submit">
</div>
</form>
</div>

</body>
</html>



Код обработчика:

<?php

$form = new ProcessForm();
$form->field_rules = array(
'field1'=>'required',
'field2'=>'required',
'field3'=>'required',
'field5'=>'number|required',
'field6'=>'number|required',
'field7'=>'required'
);
$form->validate();

class ProcessForm
{
public $field_rules;
public $error_messages;
public $fields;
private $error_list;
private $is_xhr;





function __construct()
{
$this->error_messages = array(
'required' => 'This field is required',
'email' => 'Please enter a valid email address',
'number' => 'Please enter a numeric value',
'url' => 'Please enter a valid URL',
'pattern' => 'Please correct this value',
'min' => 'Please enter a value larger than the minimum value',
'max' => 'Please enter a value smaller than the maximum value'
);

$this->field_rules = array();
$this->error_list = '';
$this->fields = $_POST;
$this->is_xhr = $this->xhr();
}





function validate()
{
if (!empty($this->fields))
{
//Validate each of the fields
foreach ($this->field_rules as $field => $rules)
{
$rules = explode('|', $rules);

foreach ($rules as $rule)
{
$result = null;

if (isset($this->fields[$field]))
{
$param = false;

if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
{
$rule = $match[1];
$param = $match[2];
}

$this->fields[$field] = $this->clean($this->fields[$field]);

//if the field is a checkbox group create string
if (is_array($this->fields[$field]))
$this->fields[$field] = implode(', ', $this->fields[$field]);

// Call the function that corresponds to the rule
if (!empty($rule))
$result = $this->$rule($this->fields[$field], $param);

// Handle errors
if ($result === false)
$this->set_error($field, $rule);
}
}
}

if (empty($this->error_list))
{
if ($this->is_xhr)
echo json_encode(array('status' => 'success'));

$this->process();
}
else
{
if ($this->is_xhr)
echo json_encode(array('status' => 'invalid', 'errors' => $this->error_list));
else echo $this->error_list;
}
}
}





function process()
{

$msg = "Form Contents: \n\n";
foreach($this->fields as $key => $field)
$msg .= "$key : $field \n";


$to = 'test@test.ru';
$subject = 'Form Submission';
$from = 'test@test.ru';


mail($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n");



}



function set_error($field, $rule)
{
if ($this->is_xhr)
{
$this->error_list[$field] = $this->error_messages[$rule];
}
else $this->error_list .= "<div class='error'>$field: " . $this->error_messages[$rule] . "</div>";
}





function xhr()
{
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false;
}





/** Validation Functions */
function required($str, $val = false)
{

if (!is_array($str))
{
$str = trim($str);
return ($str == '') ? false : true;
}
else
{
return (!empty($str));
}
}





function email($str)
{
return (!preg_match("/^(?!(?sad.gif?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?sad.gif?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?sad.gif?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?sad.gif?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?sad.gif?sad.gif?!.*[^.]{64,})(?sad.gif?sad.gif?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?sad.gif?:[a-z][a-z0-9]*)|(?sad.gif?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?sad.gif?:IPv6:(?sad.gif?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?sad.gif?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?:sad.gif?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?sad.gif?:IPv6:(?sad.gif?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}smile.gif|(?sad.gif?!(?:.*[a-f0-9]smile.gif{5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?:sad.gif?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}smile.gif?)))?(?sad.gif?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?sad.gif?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD", $str)) ? false : true;
}





function number($str)
{
return (!is_numeric($str)) ? false : true;
}





function min($str, $val)
{
return ($str >= $val) ? true : false;
}





function max($str, $val)
{
return ($str <= $val) ? true : false;
}





function pattern($str, $pattern)
{
return (!preg_match($pattern, $str)) ? false : true;
}





function clean($str)
{
$str = is_array($str) ? array_map(array("ProcessForm", 'clean'), $str) : str_replace('\\', '\\\\', strip_tags(trim(htmlspecialchars((get_magic_quotes_gpc() ? stripslashes($str) : $str), ENT_QUOTES))));
return $str;
}
}


?>


ЗАРАНЕЕ СПАСИБО!!!
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.