联系表单发送但不显示错误或成功消息

时间:2013-08-06 14:28:33

标签: php jquery forms email contact

我为联系表单制作了一个wordpress短代码,我的表单显示并发送正常,但由于某种原因它没有显示我的错误或是否已发送消息?

            <?php
            /*
            Plugin Name: contact_shortcode
            Plugin URI: http://nathanrobjohn.com
            Description: contact form. Usage: <code>[contact email="your@email.address"]</code>
            Version: 1.0
            Author: Nathan Robjohn
            Author URI:http://nathanrobjohn.com
            */


            function nathan_shortcode_contact( $atts, $content = null)  {


                extract( shortcode_atts( array(
                  'email' => get_bloginfo('admin_email')
                  ), $atts ) );

                    $content .= '
                    <script type="text/javascript"> 
                        var $j = jQuery.noConflict();
                        $j(window).load(function(){             
                            $j("#contact-form").submit(function() {
                              // validate and process form here
                                var str = $j(this).serialize();                  
                                   $j.ajax({
                                   type: "POST",
                                   url: "' . get_stylesheet_directory_uri() . '/short/sendmail.php",
                                   data: str,
                                   success: function(msg){                      
                                        $j("#note").ajaxComplete(function(event, request, settings)
                                        { 
                                            if(msg == "OK") // Message Sent? Show the Thank You message and hide the form
                                            {
                                                result = "Your message has been sent. Thank you!";
                                                $j("#fields").hide();
                                            }
                                            else
                                            {
                                                result = msg;
                                            }                                
                                            $j(this).html(result);                           
                                        });                  
                                    }                    
                                 });                     
                                return false;
                            });         
                        });
                    </script>';

                            // now we put all of the HTML for the form into a PHP string
                    $content .= '<div id="post-a-comment" class="span12 contactform">';
                        $content .= '<div id="fields">';
                            $content .= '<h1>Enquire</h1>';
                        $content .= '<div id="note"></div> <!--notification area used by jQuery/Ajax -->'; 
                            $content .= '<form id="contact-form" action="">';
                                $content .= '<input name="to_email" type="hidden" id="to_email" value="' . $email . '"/>';
                                $content .= '<p class="span2">';
                                    $content .= '<label class="error" for="name">Name *</label>';
                                    $content .= '<input name="name" type="text" id="name"/>';
                                $content .= '</p>';
                                $content .= '<p class="span2">';
                                    $content .= '<label for="email">Phone number *</label>';
                                    $content .= '<input name="phone" type="text" id="phone"/>';
                                $content .= '</p>';
                                $content .= '<p class="span2">';
                                    $content .= '<label for="subject">Best time to call</label>';
                                $content .= '<select name="time" id="subject" role="select" aria-required="true">
                                            <option></option>
                                            <option>Morning</option>
                                            <option>Afternoon</option>
                                            <option>Anytime</option>
                                        </select>   ';              
                                $content .= '</p>';     
                                $content .= '<p class="span2">';
                                    $content .= '<label for="email">E-mail address *</label>';
                                    $content .= '<input name="email" type="text" id="email"/>';
                                $content .= '</p>';
                                    $content .= '<p><label for="email">Enquiry message *</label></p>';
                                $content .= '<p class="span12"><textarea rows="16" cols="" name="message"></textarea></p>';
                                $content .= '<input type="submit" value="Submit" class="button" id="contact-submit" />';
                            $content .= '</form>';
                        $content .= '</div><!--end fields-->';
                    $content .= '</div>';
                return $content;
            }
            add_shortcode('contact', 'nathan_shortcode_contact');

上面是我的contactform.php

现在这是我的sendmail.php

            <?php
            $plugin_name = 'contact_shortcode';


            function ValidateEmail($email)
            {
                /*
                (Name) Letters, Numbers, Dots, Hyphens and Underscores
                (@ sign)
                (Domain) (with possible subdomain(s) ).
                Contains only letters, numbers, dots and hyphens (up to 255 characters)
                (. sign)
                (Extension) Letters only (up to 10 (can be increased in the future) characters)
                */

                $regex = '/([a-z0-9_.-]+)'. # name

                '@'. # at

                '([a-z0-9.-]+){2,255}'. # domain & possibly subdomains

                '.'. # period

                '([a-z]+){2,10}/i'; # domain extension 

                if($email == '') { 
                    return false;
                }
                else {
                    $eregi = preg_replace($regex, '', $email);
                }

                return empty($eregi) ? true : false;
            }

            $post = (!empty($_POST)) ? true : false;

            if($post)
            {
                $name = stripslashes($_POST['name']);
                $time = stripslashes($_POST['time']);
                $phone = stripslashes($_POST['phone']);
                $subject = 'Contact Form';
                $email = trim($_POST['email']);
                $to = trim($_POST['to_email']);
                $message = stripslashes($_POST['message']);
                $error = '';

                // Check name

                if(!$name)
                {
                    $error .= 'Please enter your name.<br />';
                }

                if(!$phone)
                {
                    $error .= 'Please enter a phone number.<br />';
                }

                if(!$time)
                {
                    $error .= 'Please select a time.<br />';
                }

                // Check email

                if(!$email)
                {
                    $error .= 'Please enter an e-mail address.<br />';
                }

                if($email && !ValidateEmail($email))
                {
                    $error .= 'Please enter a valid e-mail address.<br />';
                }

                // Check message (length)

                if(!$message || strlen($message) < 15)
                {
                    $error .= "Please enter your message. It should have at least 15 characters.<br />";
                }

                if(!$error) // send email
                {
                    $contents = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nPhone: $phone \n\nTime: $time  \n\nMessage:\n $message";
                    $mail = mail($to, $subject, $contents,
                         'From: '.$name.' <'.$email.'>\r\n'
                        .'Reply-To: '.$email.'\r\n'
                        .'X-Mailer: PHP/' . phpversion());

                    if($mail)
                    {
                        echo 'OK';
                    }

                }
                else
                {
                    echo '<div class="notification_error">'.$error.'</div>'; // set up error div for jQuery/Ajax
                }

            }
            ?>

链接到实际网站http://auto.nathanrobjohn.com/contact-us/

由于某种原因,联系表格已停止立即发送到

1 个答案:

答案 0 :(得分:0)

您需要包含wp-load.php才能使用核心WordPress功能。

使用此功能,您已将sendmail.php放在wp-content/plugins/yourplugin文件夹

中的插件文件夹中
require('../../../wp-load.php');