ThemesCreator
ThemesCreator

Reputation: 1759

jQuery code that doesn`t work on WordPress

I have a contact form that I submit via jQuery. It works perfectly on HTML/PHP/JS template, but when I use it on a WordPress it doesn't work fine.

The form is submited, but the jquery code inside the submit function is like doesn't exit. It doesn't retrieve any error but the loader doesn`t appear and jquery validation doesn't occurs:

<form method="post" action="<?php echo get_template_directory_uri() . '/templates/contact.php' ?>" name="contactform" id="contactform">
                <div class="col-sm-6">
                    <fieldset>
                        <input name="name" type="text" id="name" size="30" value="" placeholder="Name" />
                        <br />
                        <input name="email" type="text" id="email" size="30" value="" placeholder="Email" />
                        <br />
                        <input name="phone" type="text" id="phone" size="30" value="" placeholder="Phone" />
                        <br />
                    </fieldset>
                </div>
                <div class="col-sm-6">
                    <fieldset>
                        <textarea name="comments" cols="40" rows="8" id="comments" placeholder="Message"></textarea>
                        <button type="submit" class="btn btn-green-border btn-lg" id="submit" value="submit">Send Message</button>
                    </fieldset>
                </div>
            </form>

This is the jQuery:

// Send email 
    jQuery('#contactform').submit(function ($) {

        var action = $(this).attr('action');

        $("#message").slideUp(750, function () {
            $('#message').hide();

            $('#submit')
                .after('<img src="images/loader.gif" class="loader" />')
                .attr('disabled', 'disabled');

            $.post(action, {
                    name: $('#name').val(),
                    email: $('#email').val(),
                    phone: $('#phone').val(),
                    comments: $('#comments').val(),
                    verify: $('#verify').val()
                },
                function (data) {
                    document.getElementById('message').innerHTML = data;
                    $('#message').slideDown('slow');
                    $('#contactform img.loader').fadeOut('slow', function () {
                        $(this).remove()
                    });
                    $('#submit').removeAttr('disabled');
                    if (data.match('success') != null) $('#contactform').slideUp('slow');

                }
            );

        });

        return false;

    });

What can be the problem?

Upvotes: 0

Views: 124

Answers (3)

Pieter Goosen
Pieter Goosen

Reputation: 9951

I believe you are using your jquery inline.

This is how I would do it (the correct way).

  1. Create a file in your themes js folder and call it something like contactform-js.js

  2. Copy your jquery code and paste it into this new file.

  3. Register your new js file in your functions.php

This should sort some of your problems.

function register_contact_script() {
    wp_enqueue_script( 'contact-script', get_template_directory_uri() . '/js/contactform-js.js', array( 'jquery' ), '' , true );
}

add_action( 'wp_enqueue_scripts', 'register_contact_script' );

Upvotes: 0

Reece
Reece

Reputation: 396

Your Wordpress installation could be using a version of jQuery that is incompatible with your code, try including a newer version

Upvotes: 0

mack
mack

Reputation: 112

    jQuery('#contactform').submit(function ($) {

Try removing the $ in function()

    jQuery('#contactform').submit(function () {

Upvotes: 1

Related Questions