WordPress Ajax custom contact form cannot find url

i was messing around many hours with my custom form for a wordpress website and i cant figure it out how to properly connect ajax to my contact.php, here is my code! I appreciate any help! Thank you!

HTML

Read More
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/ajax_submit.js"></script>

            <form id="feedback" action="<?php bloginfo('template_url'); ?>/contact.php" enctype="multipart/form-data" method="post">

                <div id="response" class="success" style="display:none;"></div>

                <div class="inputs">

                    <input name="name" type="text" class="required" id="name" placeholder="Имя">
                </div>

                <div class="inputs">

                    <input name="email" type="text" class="required" id="email" placeholder="Электронная почта">
                </div>

                <div class="inputs">

                    <textarea name="message" class="required" id="message" placeholder="О чём бы Вы с нами хотели поговорить?"></textarea>
                </div>

                <div class="button">
                    <input type="submit" name="submit" id="submit" value="Submit" />
                </div>

                <div class="inputs">
                    <input type="hidden" name="honeypot" id="honeypot" value="http://" />
                    <input type="hidden" name="humancheck" id="humancheck" class="clear" value="" />
                </div>
            </form>

Ajax

$(document).ready(function() {

$('form #response').hide();

$('#submit').click(function(e) {

    // prevent forms default action until
    // error check has been performed
    e.preventDefault();

    // grab form field values
    var valid = '';
    var required = ' is required.';
    var name = $('form #name').val();
    var email = $('form #email').val();
    var message = $('form #message').val();
    var honeypot = $('form #honeypot').val();
    var humancheck = $('form #humancheck').val();

    // perform error checking
    if (name = '' || name.length <= 2) {
        valid = '<p>Your name' + required +'</p>';
    }

    if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+.[a-z]{2,4}$)/i)) {
        valid += '<p>Your email' + required +'</p>';
    }

    if (message = '' || message.length <= 5) {
        valid += '<p>A message' + required + '</p>';
    }

    if (honeypot != 'http://') {
        valid += '<p>Spambots are not allowed.</p>';
    }

    if (humancheck != '') {
        valid += '<p>A human user' + required + '</p>';
    }

    // let the user know if there are erros with the form
    if (valid != '') {

        $('form #response').removeClass().addClass('error')
            .html('<strong>Please correct the errors below.</strong>' +valid).fadeIn('fast');
    }
    // let the user know something is happening behind the scenes
    // serialize the form data and send to our ajax function
    else {

        $('form #response').removeClass().addClass('processing').html('Processing...').fadeIn('fast');

        var formData = $('form').serialize();
        submitForm(formData);
    }

});
});

// make our ajax request to the server
function submitForm(formData) {

jQuery.ajax({
    type: 'POST',
    url: 'contact.php',
    data: formData,
    dataType: 'json',
    cache: false,
    timeout: 7000,
    success: function (data) {

        $('form #response').removeClass().addClass((data.error === true) ? 'error' : 'success')
            .html(data.msg).fadeIn('fast');

        if ($('form #response').hasClass('success')) {

            setTimeout("$('form #response').fadeOut('fast')", 5000);
        }

    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {

        $('form #response').removeClass().addClass('error')
            .html('<p>There was an<strong> ' + errorThrown +
                '</strong> error due to a<strong> ' + textStatus +
                '</strong> condition.</p>').fadeIn('fast');
    },
    complete: function(XMLHttpRequest, status) {

        $('form')[0].reset();
    }
});
};

PHP

 function getEmail(){

 sleep(3);

 $name = trim($_POST['name']);
 $email = trim($_POST['email']);
 $message = trim($_POST['message']);
 $honeypot = $_POST['honeypot'];
 $huancheck = $_POST['humancheck'];

 if ($honeypot == 'http://' && empty($humancheck)) {

 $error_message = '';
 $reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,4}$/";

 if (!preg_match($reg_exp, $email)) {
    $error_message .= "<p> A valid email is required</p>";
 }

 if (empty($name)) {
    $error_message .= "<p>Please provide your name</p>";
 }

if (empty($message)) {
    $error_message .= "<p>A message is required.</p>";
}

if (!empty($error_message)) {
    $return['error'] = true;
    $return['msg'] = "<h3>Oops! The request was successful but your form is not filled out correctly.</h3>".$error_message;
    echo json_encode($return);
    exit();

} else {

    $to = 'kaijin2010@gmail.com';
    $headers = 'RNO-Version: 1.0' . 'n';
    $headers .= 'From: $from' . 'n';
    $subject = 'Contact Form Submissionn';
    $body = 'Name: ' .$name . 'n';
    $body .= 'Email: ' . $email . 'n';
    $body .= 'Message: ' .$message . 'n';
}

 } else {
$return['error'] = true;
$return['msg'] = "<h3>Oops! There was a problem with your submission. Please try again.</h3>";
echo json_encode($return);
}
}

Related posts

Leave a Reply