Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
<?php/*    Template Name: Contact  */?>
<?php get_header(); ?>
    <?php    
      wp_enqueue_script( 'jquery.validate.min', get_bloginfo('template_directory') . '/js/jquery.validate.min.js', array('jquery') , 3.1, true );
      wp_enqueue_script( 'jquery.form', get_bloginfo('template_directory') . '/js/jquery.form.js');
      wp_enqueue_script( 'contact', get_bloginfo('template_directory') . '/js/contact.js'); 
     wp_enqueue_style('contactstyle', get_template_directory_uri() . '/css/contactstyle.css');

    ?>      
    <div id="wrap">
                <form id="contactform" action="processForm.php" method="post">
                <table>
                    <tr>
                        <td><label for="name">Name:</label></td>
                        <td><input type="text" id="name" name="name" /></td>
                    </tr>
                    <tr>
                        <td><label for="email">Email:</label></td>
                        <td><input type="text" id="email" name="email" /></td>
                    </tr>
                    <tr>
                        <td><label for="message">Message:</label></td>
                        <td><textarea id="message" name="message" rows="5" cols="20"></textarea></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><input type="submit" value="Send!" id="send" /></td>
                    </tr>
                </table>
            </form>
            <div id="response"></div>
 </div>

also i have the processForm.php

   <?php
    // Clean up the input values
    foreach($_POST as $key => $value) {
        if(ini_get('magic_quotes_gpc'))
            $_POST[$key] = stripslashes($_POST[$key]);

        $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
    }

    // Assign the input values to variables for easy reference
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];

    // Test input values for errors
    $errors = array();
    if(strlen($name) < 2) {
        if(!$name) {
            $errors[] = "You must enter a name.";
        } else {
            $errors[] = "Name must be at least 2 characters.";
        }
    }
    if(!$email) {
        $errors[] = "You must enter an email.";
    } else if(!validEmail($email)) {
        $errors[] = "You must enter a valid email.";
    }
    if(strlen($message) < 10) {
        if(!$message) {
            $errors[] = "You must enter a message.";
        } else {
            $errors[] = "Message must be at least 10 characters.";
        }
    }

    if($errors) {
        // Output errors and die with a failure message
        $errortext = "";
        foreach($errors as $error) {
            $errortext .= "<li>".$error."</li>";
        }
        die("<span class='failure'>The following errors occured:<ul>". $errortext ."</ul></span>");
    }

    // Send the email
    $to = "MY-EMAIL";
    $subject = "Contact Form: $name";
    $message = "$message";
    $headers = "From: $email";

    mail($to, $subject, $message, $headers);

    // Die with a success message
    die("<span class='success'>Success! Your message has been sent.</span>");

    // A function that checks to see if
    // an email is valid
    function validEmail($email)
    {
       $isValid = true;
       $atIndex = strrpos($email, "@");
       if (is_bool($atIndex) && !$atIndex)
       {
          $isValid = false;
       }
       else
       {
          $domain = substr($email, $atIndex+1);
          $local = substr($email, 0, $atIndex);
          $localLen = strlen($local);
          $domainLen = strlen($domain);
          if ($localLen < 1 || $localLen > 64)
          {
             // local part length exceeded
             $isValid = false;
          }
          else if ($domainLen < 1 || $domainLen > 255)
          {
             // domain part length exceeded
             $isValid = false;
          }
          else if ($local[0] == '.' || $local[$localLen-1] == '.')
          {
             // local part starts or ends with '.'
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $local))
          {
             // local part has two consecutive dots
             $isValid = false;
          }
          else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
          {
             // character not valid in domain part
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $domain))
          {
             // domain part has two consecutive dots
             $isValid = false;
          }
          else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                     str_replace("\\\\","",$local)))
          {
             // character not valid in local part unless 
             // local part is quoted
             if (!preg_match('/^"(\\\\"|[^"])+"$/',
                 str_replace("\\\\","",$local)))
             {
                $isValid = false;
             }
          }
          if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
          {
             // domain not found in DNS
             $isValid = false;
          }
       }
       return $isValid;
    }

    ?>

and the contact.js

  $(function() {
        // Validate the contact form
      $('#contactform').validate({
        // Specify what the errors should look like
        // when they are dynamically added to the form
        errorElement: "label",
        wrapper: "td",
        errorPlacement: function(error, element) {
            error.insertBefore( element.parent().parent() );
            error.wrap("<tr class='error'></tr>");
            $("<td></td>").insertBefore(error);
        },

        // Add requirements to each of the fields
        rules: {
            name: {
                required: true,
                minlength: 2
            },
            email: {
                required: true,
                email: true
            },
            message: {
                required: true,
                minlength: 10
            }
        },

        // Specify what error messages to display
        // when the user does something horrid
        messages: {
            name: {
                required: "Please enter your name.",
                minlength: jQuery.format("At least {0} characters required.")
            },
            email: {
                required: "Please enter your email.",
                email: "Please enter a valid email."
            },
            message: {
                required: "Please enter a message.",
                minlength: jQuery.format("At least {0} characters required.")
            }
        },

        // Use Ajax to send everything to processForm.php
        submitHandler: function(form) {
            $("#send").attr("value", "Sending...");
            $(form).ajaxSubmit({
                success: function(responseText, statusText, xhr, $form) {
                    $(form).slideUp("fast");
                    $("#response").html(responseText).hide().slideDown("fast");
                }
            });
            return false;
        }
      });
    });

But the form only loading, and never sent the email. Also, o create a folder call php and put there the processForm.php but the result is the same. Any help for this? Thanks in advanced.

Updates:

1st I used the $.noConflict(); method with this uses:

$.noConflict();
$(function() 
{
    // Validate the contact form
   $('#contactform').validate({

but i see an 404 error page. processForm.php 2nd: jQuery(document).ready(function($){ but not load the send email

share|improve this question

1 Answer 1

up vote 2 down vote accepted

From your code:

 $(function() {
        // Validate the contact form
      $('#contactform').validate({

WordPress loads jQuery in "no conflict" mode, meaning that the "$" symbol is not defined.

Change the "$" to the proper full "jQuery" or use a no conflict wrapper.

share|improve this answer
    
$(document).ready(function() { // Validate the contact form $('#contactform').validate({ but anything cant happen. –  MikeD Feb 26 '13 at 18:18
    
Did you read the Codex page section Otto linked? –  Chip Bennett Feb 26 '13 at 18:22
    
yes Chip Bennett –  MikeD Feb 26 '13 at 18:27
1  
You might go back and look again. $(document).ready(function() is not what is recommended in that link. Instead, use jQuery(document).ready(function($). –  Chip Bennett Feb 26 '13 at 18:57
    
Chip Bennett i read the article, again and again. If you read the first post, i write some updates. The problem remains. –  MikeD Feb 26 '13 at 19:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.