Ajax Contact form plugin

时间:2014-03-06 作者:ashraf

我面临着一个奇怪的问题。我正在构建ajax联系人表单插件。我的问题当我在主题页脚中添加jquery脚本时,一切都正常。但如果我从插件目录添加相同的脚本,它将不再工作。我的代码添加脚本。请注意,如果ace。js添加在脚本标记的页脚中

这是来源https://www.dropbox.com/s/8m64xzueohppuxd/ajax_contactform.zip

add_action( \'wp_enqueue_scripts\',\'webangon_ajax_contact\'); 
function webangon_ajax_contact() {
wp_enqueue_script(\'ajcontact\', plugins_url("/inc/ace.js", __FILE__),array(jquery));}
表单部分

    <form method="post" id="mailinglist" action="">
<div id="message"></div>
<input type="text" name="mailinglistname" id="mailinglistname" placeholder="John Doe" /></br>
<input type="text" name="mailinglistemail" id="mailinglistemail" placeholder="[email protected]" /></br>
<textarea name="mailinglistcomment" id="mailinglistcomment" cols="58" rows="5" tabindex="4" placeholder="Do you develope websites for FREE?"></textarea></br>
<input type="submit" name="submit" class="read-more" id="mailinglistsubmit" value="Send" /><img src="<?php bloginfo(\'template_directory\'); ?>/image/89.gif" alt="" class="ajaxsave" style="display: none;" />
</form>
jquery部件ace。js公司

jQuery(document).ready(function(){
jQuery("#mailinglist").submit(function()    {
    if(jQuery("#mailinglistname").val()=="") {
        jQuery("#mailinglist #message").text("Please enter your name.");
        return false;
    }       
    if(jQuery("#mailinglistemail").val()=="") {
        jQuery("#mailinglist #message").text("Please enter your email address.");
        return false;
    } else {
        var email = jQuery(\'#mailinglistemail\').val();
        var name = jQuery(\'#mailinglistname\').val();
        var comment = jQuery(\'#mailinglistcomment\').val();
        if(email.indexOf("@") == -1 || email.indexOf(".") == -1) {
            jQuery("#mailinglist #message").text("Please enter a valid email address.");
            return false;
        } else {
            var data = {
                action: \'join_mailinglist\',
                email: email,
                name:name,
                comment:comment,
            };
            jQuery("#mailinglistsubmit").hide();
            jQuery(".ajaxsave").show();
            jQuery.post("<?php echo admin_url(\'admin-ajax.php\'); ?>", data,
            function(response){
                jQuery(".ajaxsave").hide();
                jQuery("#mailinglistsubmit").show();
                jQuery("#mailinglist #message").html(response);
            });     
            return false;
        }
    } 

});
});

最后是ajax管理部分

add_action(\'wp_ajax_join_mailinglist\', \'join_mailinglist_callback\');
add_action(\'wp_ajax_nopriv_join_mailinglist\', \'join_mailinglist_callback\');
function join_mailinglist_callback() {
$email = $_POST[\'email\'];
$name = $_POST[\'name\'];
$comment = $_POST[\'comment\'];
$emailTo = "[email protected]";
$body = "Name: $name Email: $email Comment: $comment";
if(wp_mail($emailTo, $name,$body)){
echo "<div class=\'success\'>Enquiry has been sent successfully</div>";
}
else
{
echo "<div class=\'error\'>Please Try Again</div>";
}
die();
}

1 个回复
最合适的回答,由SO网友:czerspalace 整理而成

wp_register_script(\'ajcontact\', plugins_url("/inc/ace.js", __FILE__),array(\'jquery\'));
wp_localize_script(\'ajcontact\', \'MyAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ));
wp_enqueue_script(\'ajcontact\');
然后是你的王牌。js do公司

jQuery.post(MyAjax.ajaxurl, data, ....

结束

相关推荐