Good morning!

The question is how to implement the function of closing a submission form and calling another form after executing the .POST method using ajax. Also, if you tell a newbie about his mistakes, I will be very grateful :)

There is such:

  1. Form validator:

    $(document).ready(function() { var patternmail = /^[a-z0-9_-]+@[a-z0-9-]+\.[az]{2,6}$/i; var mail = $('#mail'); var name = $('#firstname'); var lname = $('#lastname'); var phone = $('#phone'); var text = $('#text'); var allInputs = $('#mail, #firstname, #lastname, #phone, #text'); $('.alert-danger').hide(1); allInputs.on('input', function() { var resolution = true; if (mail.val() != '') { if (mail.val().search(patternmail) == 0) { $('#alertmail').hide(1) $('#submit').attr('disabled', false); mail.removeClass('error').addClass('ok'); $('#mail').css('border', 0); } else { $('#alertmail').show(1) $('#mail').css('border', '2px solid #cc3b3b'); mail.addClass('ok'); resolution = false; } } else { mail.addClass('error'); resolution = false; } if (name.val() != '') { if (name.val().length >= 3) { $('#alertname').hide(1) name.removeClass('error').addClass('ok'); $('#firstname').css('border', 0); } else { $('#alertname').show(1) $('#firstname').css('border', '2px solid #cc3b3b'); resolution = false; name.addClass('ok'); } } else { name.addClass('error'); resolution = false; } if (lname.val() != '') { if (lname.val().length >= 3) { $('#alertlname').hide(1) lname.removeClass('error').addClass('ok'); $('#lastname').css('border', 0); } else { $('#alertlname').show(1) $('#lastname').css('border', '2px solid #cc3b3b'); resolution = false; lname.addClass('ok'); } } else { lname.addClass('error'); resolution = false; } if (phone.val() != '') { if (phone.val().length >= 9) { $('#alertphone').hide(1) phone.removeClass('error').addClass('ok'); $('#phone').css('border', 0); } else { $('#alertphone').show(1) $('#phone').css('border', '2px solid #cc3b3b'); resolution = false; phone.addClass('ok'); } } else { phone.addClass('error'); resolution = false; } if (text.val() != '') { if (text.val().length >= 10) { $('#alerttext').hide(1) text.removeClass('error').addClass('ok'); $('#text').css('border', 0); } else { $('#alerttext').show(1) $('#text').css('border', '2px solid #cc3b3b'); resolution = false; text.addClass('ok'); } } else { text.addClass('error'); resolution = false; }; if (resolution === true) { $('#submit').removeAttr('disabled'); } else { $('#submit').attr('disabled', ''); }; }); }); 
  2. The execution of the POST method, the opening of the modal is registered there as well - the sending form disappears, but the modal does not open.

     function call() { var msg = $('#formx').serialize(); $.ajax({ type: 'POST', url: 'post.php', data: msg, success: function(data) { $('#contact').hide(1); $('#successmodal').show(1); $('#successmodal').html(data); }, error: function(xhr, str){ alert('Возникла ошибка: ' + xhr.responseCode); } }); } 
    1. Post.php

     <strong><? $send = $_POST['button']; $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $mail = $_POST['mail']; $phone = $_POST['phone']; $text = $_POST['text']; if ($send != "button"){ $to = "meowwiza@gmail.com"; // <- Ваша почта $subject = "Письмо с сайта"; // <- Заголовок письма на почте $mailheaders .="От: $mail"; $msg .= "ФИО отправителя: $firstname $lastname\r\n"; $msg .= "E-mail отправителя: $mail\r\n"; $msg .= "Телефон: $phone\r\n"; $msg .= "Сообщение: $text\r\n"; mail ($to, $subject, $msg, $mailheaders); $mess= "Уважаемый $firstname, ваше сообщение было успешно отправленно."; echo '<center><p class="text">' . $mess ; } ?></strong> 

Our modalka with the form:

 <div id="contact" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> <form id="formx" method="post" action="javascript:void(null);" onsubmit="call()"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">Обратная связь</h4> </div> <div class="modal-body"> <div class="row"> <div class="col-md-6"> <div class="alert alert-danger" id="alertname" role="alert">Имя введено не корректно.</div> </div> <div class="col-md-6"> <div class="alert alert-danger" id="alertlname" role="alert">Введите фамилию.</div> </div> </div> <div class="row"> <div class="col-md-6"><label class="name">Имя*</label></div> <div class="col-md-6 hidden-xs hidden-sm"><label class="name">Фамилия*</label></div> <div class="col-md-6"><input type="text" class="name" name="firstname" id="firstname"></div> <div class="col-md-6 hidden-md hidden-lg"><label class="name">Фамилия*</label></div> <div class="col-md-6"><input type="text" class="name" name="lastname" id="lastname"></div> </div> <div class="row"> <div class="col-md-12"> <div class="alert alert-danger" id="alertmail" role="alert">Введите адрес электронной почты.</div> </div> </div> <div class="row"> <div class="col-md-12"><label class="name">Почта*</label></div> <div class="col-md-12"><input type="email" class="mail" name="mail" id="mail"></div> </div> <div class="row"> <div class="col-md-12"> <div class="alert alert-danger" id="alertphone" role="alert">Введите телефон.</div> </div> </div> <div class="row"> <div class="col-md-12"><label class="name">Телефон*</label></div> <div class="col-md-12"><input type="text" class="mail" name="phone" id="phone"></div> </div> <div class="row"> <div class="col-md-12"> <div class="alert alert-danger" id="alerttext" role="alert">Введите сообщение.</div> </div> </div> <div class="row"> <div class="col-md-12"><label class="name">Сообщение*</label></div> <div class="col-md-12"><textarea name="text" id="text" cols="30" rows="10"></textarea></div> </div> </div> <div class="modal-footer"> <input class="btn btn-primary" id="submit" type="submit" value="Отправить"> </div> </form> </div> </div> </div> 

To make it clearer, I attach screenshots and the site itself:

http://test.truskahouses.in.ua/pura/

The form:

enter image description here

After sending the form closes and leaves the page in this form:

enter image description here

Closed due to the fact that off-topic party Nick Volynkin Jul 7 '17 at 2:16 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Nick Volynkin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin
  • There is a lot of code in the question, it is not clear what concerns the problem from it. Try to make a minimal, self-sufficient and reproducible example . - Nick Volynkin
  • A link to your own website should not be required to understand. In a week, your site will change and the link will become useless for the question. Read more: Q: My site / project is not working. Can I just give him a link? - Nick Volynkin
  • @NickVolynkin Well, why are you such a spite, doesn’t inspection code mean that the code can be long? - user234223
  • inspection means that there are no errors in the code, and you are just asking for errors to be found. ) - Nick Volynkin

1 answer 1

It seems that you are banally canceling sending a message to soap.

 $send = $_POST['button']; if ($send != "button"){ 

Try to fix on

 $send = $_POST['button']; if ($send == "button"){ 
  • Nah, now even the text is not displayed by the _POST $ mess = method "Dear $ firstname, your message was successfully sent."; - user234223
  • Give your input (which type = send) the name = "button" should work - Klimenkomud
  • Alas, the letters still do not come. Hosting shows that sent 0 letters from 500 per day. - user234223
  • imgur.com/a/KXjdR - user234223
  • @ user234223 Try var_dump (mail ($ to, $ subject, $ msg, $ mailheaders)); - Klimenkomud