D
D
danil_linkins2017-06-07 21:35:54
PHP
danil_linkins, 2017-06-07 21:35:54

How to remove the redirect to the send file when submitting the form?

On the site johny-gyro.ru , the form is sent by Ajax, that is, without reloading the page.
But there is a separate place where it does not work - the product window on all three products, after sending the site immediately redirects to the sender's php file, which is specified in the action. (the product window itself is made with jquery product quick view)

Here is the form:

<form method="POST" class="mail-to" action="mail.php">
                  <input type="text" name="phone" required placeholder="Телефон">
                  <input class="my-btn yellow-my-btn" type="submit" value="Отправить">
                </form>


Handler script:
$("form").submit(function(e) { //устанавливаем событие отправки для формы с id=form
        e.preventDefault();
        var form = $(this);
        var form_data = $(this).serialize(); //собераем все данные из формы
        $.ajax({
            type: "POST", //Метод отправки
            url: form.attr('action'), //путь до php фаила отправителя
            data: form_data,
            success: function() {
              // yaCounter43092004.reachGoal(goal);
              //код в этом блоке выполняется при успешной отправке сообщения
              $('#thankyouModal').modal('show');
              form[0].reset();
    }});
    });


And mail php:
<?php
// несколько получателей
$to  = '[email protected]';  // обратите внимание на запятую

// тема письма
$subject = 'Заявка на сайте johny-gyro (' . date("Y-m-d H:i:s") . ')';

// текст письма меняется он!!
$message = '' . $_POST['phone'];

// Для отправки HTML-письма должен быть установлен заголовок Content-type
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit' . "\r\n"; 

// Дополнительные заголовки
$headers .= 'To: info <[email protected]>' . "\r\n"; // Свое имя и email
$headers .= 'From: [email protected] ' . "\r\n";

// Отправляем
mail($to, $subject, $message, $headers);
?>


A live example can be viewed on the site itself.
Tell me, please, who worked with this, maybe I made a mistake where.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel, 2017-06-07
@mrusklon

where is the code for all forms? may not be specified type submit .. little information

X
Xfer, 2017-06-07
@Xfer

Remove "action" and send you to mail.php
url: ('mail.php')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question