WordPress AJAX форма только пост дает ошибку JQuery? - PullRequest
0 голосов
/ 01 ноября 2018

У меня есть WordPress-сайт, на котором есть форма подписки на новостную рассылку ajax. На главной странице все работает без ошибок :-) На странице поста в блоге я получаю эту ошибку:

[Ошибка] TypeError: e.indexOf не является функцией. (В e.indexOf ("") "e.indexOf не определено) (анонимная функция) (jquery-3.3.1.min.js: 2: 31048)

[Error] TypeError: a.parent ("a"). Size не является функцией. (В «a.parent (« a »). Size ()», «a.parent (« a »). Size» не определено) (анонимная функция) (jquery-3.3.1.min.js: 2: 31048)

HTML:

<div class="newsletter-section">
        <div id="form-messages"></div>

    <div class="form-div d-flex justify-content-center">
        <form id="ajax-contact" method="POST" action="mailer.php">

            <input type="text" class="" id="email" name="email" required placeholder="Sign up for our newsletter">
            <div class="btn-center">
                <button type="submit">Sign up</button>
            </div>
        </form>

    </div>  
</div>

код:

$(function() {

// Get the form.
var form = $('#ajax-contact');

// Get the messages div.
var formMessages = $('#form-messages');

// Set up an event listener for the contact form.
$(form).submit(function(e) {
    // Stop the browser from submitting the form.
    e.preventDefault();

    // Serialize the form data.
    var formData = $(form).serialize();

    // Submit the form using AJAX.
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: formData
    })
    .done(function(response) {
        // Make sure that the formMessages div has the 'success' class.
        $(formMessages).removeClass('error');
        $(formMessages).addClass('success');

        // Set the message text.
        $(formMessages).text(response);

        // Clear the form.
        $('#email').val('');
    })
    .fail(function(data) {
        // Make sure that the formMessages div has the 'error' class.
        $(formMessages).removeClass('success');
        $(formMessages).addClass('error');

        // Set the message text.
        if (data.responseText !== '') {
            $(formMessages).text(data.responseText);
        } else {
            $(formMessages).text('Oops! Er loopt iets fout. Je boeking is niet verzonden.');
        }
    });

});

});

Кто-нибудь может мне помочь?

Спасибо, Фред

1 Ответ

0 голосов
/ 02 ноября 2018

Казалось, что я должен был указать абсолютный путь к php-скрипту в форме. Работает безупречно! Спасибо за внимание.

...