У нас есть форма
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<div class="services_form_right"> <form class="services_form" id="paving_and_laying"> <input type="text" name="name" placeholder="Имя"> <input type="email" name="email" placeholder="E-mail"> <input type="tel" name="phone" placeholder="Телефон"> <textarea name="message" placeholder="Комментарий к заказу"></textarea> <div class="box_agree"> <input type="checkbox" name="agree" id="agree" class="checkbox"> <label for="agree" class="check">Даю согласие на обработку персональных данных</label> </div> <div class="services_btn"> <button type="submit" class="btn">Расчитать заказ</button> </div> </form> </div> |
js’ка
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
$("form#paving_and_laying").on('submit', function(event){ event.preventDefault(); form = $(this); $.ajax({ type: "POST", url: "/make.php", data: $(this).serializeArray(), success: function(msg) { json = $.parseJSON(msg); form.find('.requiredInputError').removeClass('requiredInputError'); if (json.result) { form.parent('.services_form_right').addClass('success').html(json.msg); form.remove(); } else { if (json.errors) { json.errors.forEach(function(item) { if (item == 'agree') { form.children('div').children('label').addClass('requiredInputError'); } else if (item == 'message') { form.children('textarea[name="message"]').addClass('requiredInputError'); } else { form.children('input[name="' + item + '"]').addClass('requiredInputError'); } }); } else { alert(json.msg); } } } }); }); |
Файл make.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
<?php $name = $_REQUEST['name']; $email = $_REQUEST['email']; $phone = $_REQUEST['phone']; $message = $_REQUEST['message']; $agree = isset($_REQUEST['agree']); $errors = false; $requiredFields = ['name', 'email', 'phone', 'agree', 'message']; $errorFields = []; // проверяем обязательные поля foreach($requiredFields as $field){ if (!$_REQUEST[$field]){ array_push($errorFields, $field); $errors = true; } } if (!$errors) : $subjectuser = "Сообщение"; $headersuser = "From: Сообщение\r\n"; $headersuser .= "Reply-To: ". strip_tags($name) . "\r\n"; $headersuser .= "MIME-Version: 1.0\r\n"; $headersuser .= "Content-Type: text/html;charset=utf-8 \r\n"; $my_message = "<h1>ТЕМА</h1><br/>"; $my_message .= "Имя: {$name}<br/>"; if ($phone){ $my_message .= "Телефон: {$phone}<br />"; } if ($email){ $my_message .= "E-mail: {$email}<br />"; } if ($message){ $my_message .= "Комментарий: <br>" . $message . "<hr/>"; } $my_message .= "Время заказа: " . date('H:i:s d.m.Y'); require_once($_SERVER['DOCUMENT_ROOT'] . '/include/libs/PHPMailer/class.phpmailer.php'); //Подключаем PHPMailer $mail = new PHPMailer(true); //New instance, with exceptions enabled $mail->CharSet = "UTF-8"; $mail->IsSMTP(); // telling the class to use SMTP $mail->Host = "smtp.yandex.ru"; // SMTP server $mail->SMTPDebug = 0; // enables SMTP debug information (for testing) $mail->SMTPAuth = true; // enable SMTP authentication $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->SMTPSecure = 'ssl'; // Secure SMTP $mail->Username = 'from@matveevs,ru'; // SMTP account username $mail->Password = "fromPassword"; // SMTP account password $mail->SetFrom('from@matveevs,ru', 'site.ru'); $mail->AddReplyTo('from@matveevs,ru','site.ru'); $mail->Subject = "Сообщение с сайта site.ru"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail->MsgHTML($my_message); $address = 'to@matveevs.ru'; $mail->AddAddress($address, "User"); $mail->CharSet="UTF-8"; $mail->IsHTML(true); // send as HTML if(!$mail->Send()) : $res = Array( "result" => false, "msg" => "Ошибка отправления" ); else: $res = Array( "result" => true, "msg" => "Спасибо! Ваша заявка отправлена" ); require $_SERVER["DOCUMENT_ROOT"] . '/addToBitrix.php'; endif; else: $res = Array( "result" => false, "msg" => "Ошибка: заполните все поля формы", "errors" => $errorFields ); endif; echo json_encode($res, JSON_UNESCAPED_UNICODE); |
addToBitrix.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php require $_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/main/include/prolog_before.php'; CModule::IncludeModule("catalog"); $el = new CIBlockElement; $PROP = array( "NAME" => $name, "PHONE" => $phone, "EMAIL" => $email, "MESSAGE" => $message ); $arFields = array( "IBLOCK_ID" => 10, "IBLOCK_SECTION_ID" => false, "NAME" => $name, "PROPERTY_VALUES" => $PROP, "ACTIVE" => "Y", ); $el->Add($arFields); |