Last active
August 1, 2019 18:35
-
-
Save luckydevilru/8dd8fef99331d698b39fa3a4a5714062 to your computer and use it in GitHub Desktop.
Отправка формы ajax без перезагрузки страницы
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // html | |
| <form class="block mbr-form" method="post"> | |
| <div class="form-rezult"></div> | |
| <div class="row"> | |
| <div class="col-md-6 multi-horizontal" data-for="name"> <input type="text" class="form-control input" name="DATA[NAME]" data-form-field="Name" placeholder="Ваше Имя:" required="" id="name-form4-1b"> </div> | |
| <div class="col-md-6 multi-horizontal" data-for="phone"> <input type="text" class="form-control input" name="DATA[PHONE]" data-form-field="Phone" placeholder="Телефон:" required="" id="phone-form4-1b"> </div> | |
| <div class="col-md-12" data-for="email"> <input type="text" class="form-control input" name="DATA[ЕMAIL]" data-form-field="Email" placeholder="Email:" required="" id="email-form4-1b"> </div> | |
| <div class="col-md-12" data-for="message"> <textarea class="form-control input" name="DATA[MESSAGE]" rows="3" data-form-field="Message" placeholder="Сообщение:" style="resize:none" id="message-form4-1b"></textarea> </div> | |
| <div class="input-group-btn col-md-12" style="margin-top: 10px;"><button href="" type="submit" class="btn btn-primary btn-form display-4"><span class="mbri-chat mbr-iconfont mbr-iconfont-btn"></span>ОТПРАВИТЬ СООБЩЕНИЕ</button></div> | |
| </div> | |
| </form> | |
| // js | |
| $(document).ready(function() { | |
| $("form").bind('submit', function(e) { | |
| e.preventDefault(); | |
| $.ajax({ | |
| url: 'order.php', // php обработчик (код ниже) | |
| type: "POST", //метод отправки | |
| dataType: "html", //формат данных | |
| data: $(this).serialize(), // Сеарилизуем объект | |
| beforeSend: function(data) { // сoбытиe дo oтпрaвки | |
| $(this).find('input[type="submit"]').attr('disabled', 'disabled'); // нaпримeр, oтключим кнoпку, чтoбы нe жaли пo 100 рaз | |
| }, | |
| success: function(data) { // сoбытиe пoслe удaчнoгo oбрaщeния к сeрвeру и пoлучeния oтвeтa | |
| if(!data.match(/Спасибо/)) { // eсли oбрaбoтчик вeрнул oшибку | |
| $('.form-rezult').html(data).removeClass("alert-success").addClass('alert alert-form alert-danger text-center'); // пoкaжeм eё тeкст | |
| } else { // eсли всe прoшлo oк | |
| $('.form-rezult').html(data).removeClass("alert-danger").addClass('alert alert-form alert-success text-center'); // пишeм чтo всe oк | |
| } | |
| }, | |
| error: function(xhr, ajaxOptions, thrownError) { // в случae нeудaчнoгo зaвeршeния зaпрoсa к сeрвeру | |
| $('.form-rezult').html(xhr.status).removeClass("alert-success").addClass('alert alert-form alert-danger text-center'); // пoкaжeм oтвeт сeрвeрa | |
| $('.form-rezult').html(thrownError).removeClass("alert-success").addClass('alert alert-form alert-danger text-center'); // и тeкст oшибки | |
| }, | |
| complete: function(data) { // сoбытиe пoслe любoгo исхoдa | |
| $(this).find('input[type="submit"]').prop('disabled', false); // в любoм случae включим кнoпку oбрaтнo | |
| } | |
| }); | |
| return false; | |
| }); | |
| }); | |
| //php | |
| <?php | |
| $ip = $_SERVER['HTTP_X_REAL_IP']; | |
| $leadData2 = $_POST['DATA']; | |
| $phone = preg_replace('/\s|\+|-|\(|\)/', '', $leadData2['PHONE']); // удалим пробелы, и прочие не нужные знаки | |
| if (strlen($phone) !== 10 && strlen($phone) !== 11) { | |
| die('Введите правильно Телефон!'); | |
| } | |
| elseif (!preg_match("/^(?:[a-z0-9]+(?:[-_.]?[a-z0-9]+)?@[a-z0-9_.-]+(?:\.?[a-z0-9]+)?\.[a-z]{2,5})$/i", $leadData2['ЕMAIL'])) { | |
| die('Введите правильно Email!'); | |
| } | |
| else { | |
| // get lead data from the form | |
| $postData2 = array( | |
| 'Имя ' => $leadData2['NAME'], | |
| 'Телефон ' => $leadData2['PHONE'], | |
| 'Email ' => $leadData2['ЕMAIL'], | |
| 'Комментарии' => $leadData2['MESSAGE'], | |
| ); | |
| $strPostData2 = ''; | |
| foreach($postData2 as $key => $value) | |
| $strPostData2.= ($strPostData2 == '' ? '' : '<br />') . $key . ': ' . nl2br($value); | |
| $to = "[email protected]"; | |
| $subject = "Тема пиьсма"; | |
| $msg = "$strPostData2 <br /> | |
| ---- <br /> | |
| $ip<br /> | |
| <a href='$actual_link'>$actual_link</a> | |
| "; | |
| mail($to, $subject, $msg, "Content-Type: text/html; charset=utf-8 \r\n"); | |
| die('Спасибо, что написали нам! Скоро с Вами свяжетcя наш менеджер.'); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment