我有一个非常简单的表单,有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();
}
非常感谢您能帮助我了解我的错误所在。非常感谢。