AJAX表单似乎已发送,但未返回

时间:2013-10-02 作者:sosukeinu

我有一个非常简单的表单,有3个复选框,允许用户将视频标记为不合适。我试图发出一个简单的ajax请求来创建一封电子邮件,并给用户一些反馈。表单似乎已发布,信息已发送到标题中,但未返回任何内容。

我对使用ajax真的很陌生,所以我肯定我错过了一些东西。

模板文件。php:

<script type="text/javascript">
                          var ajaxurl = \'<?php echo admin_url(\'admin-ajax.php\'); ?>\';
                            $(window).load(function(){
                                $(\'[id^=report-form-]\').submit(function() {
                                       $.ajax({
                                       type: "POST",
                                       url: ajaxurl,
                                       action : "trailer_report",
                                       data: {
                                        inappropriate : \'test\',
                                        subject : $(\'#subject\',this).val(),
                                        dvdname : $(\'#dvdname\',this).val(),
                                        dvdnum : $(\'#dvdnum\',this).val(),

                                       },
                                       success: function(msg){
                                            $("#note").ajaxComplete(function(event, request, settings)
                                            {
                                                if(msg == "OK") // Message Sent? Show the Thank You message and hide the form
                                                {
                                                    result = "Your report has been sent. Thank you!";
                                                    $("#fields").hide();
                                                }
                                                else
                                                {
                                                    result = msg;
                                                }
                                                $(this).html(result);
                                            });
                                        }
                                     });
                                    return false;
                                });
                            });
                        </script>
                      <div id="reportTrailer-<?php echo $i; ?>">
                      <div id="fields">
                      <form id="report-form-<?php echo $i; ?>" class="form-inline" method="post" action="">
                              <label class="checkbox">
                                <input type="checkbox" name="inappropriate" id="inappropriate" value=""> Inappropriate
                              </label>
                              <label class="checkbox">
                                <input type="checkbox" name="incorrect" id="incorrect" value=""> Incorrect Trailer
                              </label>
                              <label class="checkbox">
                                <input type="checkbox" name="copyright" id="copyright" value=""> Copyright Infringement
                              </label>
                            <button class="btn btn-primary pull-right" type="submit" name="Submit" id="gcpl_rt_send"><i class="icon-circle-arrow-right"></i> Send Report</button>
                            <input type="hidden" name="reported" id="reported" value="true" />
                            <input type="hidden" name="subject" id="subject" value="Explore DVD Trailer Reported" />
                            <input type="hidden" name="dvdname" id="dvdname" value="<?php echo $results[$key][\'title\']; ?>" />
                            <input type="hidden" name="dvdnum" id="dvdnum" value="<?php echo $results[$key][\'stdnum\']; ?>" />
                        </form>
                      </div> <!-- endfields -->
                        <div id="note"></div>
                      </div>
功能。php:

add_action(\'wp_ajax_nopriv_trailer_report\', \'trailer_report\');
add_action(\'wp_ajax_trailer_report\', \'trailer_report\');

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

    if($post) {
        $noneError = "";
        $inappropriate = "";
        $incorrect = "";
        $copyright = "";

        if(!isset($_POST[\'inappropriate\']) && !isset($_POST[\'incorrect\']) && !isset($_POST[\'copyright\'])) {
            $noneError = \'Please select one or more reasons for reporting this trailer.\';
            $hasError = true;
            $inappropriate = "";
            $incorrect = "";
            $copyright = "";
        } else {
            if(isset($_POST[\'inappropriate\'])) {
            $inappropriate = \'Inappropriate Content\';
            }
            if(isset($_POST[\'incorrect\'])) {
            $incorrect = \'Incorrect Trailer\';
            }
            if(isset($_POST[\'copyright\'])) {
            $copyright = \'Copyright Infringement\';
            }
        }

        $subject = trim($_POST[\'subject\']);;
        $moviename = trim($_POST[\'dvdname\']);;
        $movienum = trim($_POST[\'dvdnum\']);;

        if(!isset($hasError)) {
            $emailTo = \'[email protected]\';
            if (!isset($emailTo) || ($emailTo == \'\') ){
                $emailTo = get_option(\'admin_email\');
            }
            $subject = \'The Trailer for  \'.$moviename.\' has been reported\';
            $body = "Movie Name: $moviename \\n\\nMovie Standard Number: $movienum \\n\\nReported for: $inappropriate \\n\\n$incorrect \\n\\n$copyright ";
            $headers = \'From: report <[email protected]>\' . "\\r\\n";

            $mail = wp_mail($emailTo, $subject, $body, $headers);
            $emailSent = true;

            if($mail) {
                echo \'OK\';
            }
        } else {
                echo \'<div class="notification_error">\'.$noneError.\'</div>\'; // set up error div for jQuery/Ajax
        }
    }
die();
}
非常感谢您能帮助我了解我的错误所在。非常感谢。

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

你的settings object 在ajax中,调用未正确设置:操作应插入data 对象

例如:

$.ajax({
    url: ajax_url,
    data:{
        action: \'trailer_report\',// action must be set here
        // other stuff here
    }
    //other ajax stuff
});
希望有帮助!

结束

相关推荐

WP AJAX在“CURRENT_SCREEN”管理挂钩中不起作用

我有一个类正在做一些后端工作(创建css和js文件,创建元盒,注册ajax挂钩等等)当我只包含文件时,一切都很好。问题是这些文件包含在每个管理页面上。为了只在我真正想要的页面上包含它们(CPT),我使用了\'current_screen\' 管理挂钩。在那个钩子里使用很安全\'get_current_screen\' function 我使用该函数确定当前所在的页面,以便添加适当的类。接下来发生的事情是,我的类仅在CPT页面上继续按预期工作(生成css、js、创建元盒),但ajax挂钩停止工作(响应0)。