I'm trying to 'ajaxify' menting in WordPress using this tutorial Ajaxify WordPress Comments
Here is my PHP handler:
function ajaxify_ments( $ment_ID, $ment_status ){
if( ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
//If AJAX Request Then
switch( $ment_status ) {
case '0':
//notify moderator of unapproved ment
wp_notify_moderator( $ment_ID );
case '1': //Approved ment
echo "success";
$mentdata = &get_ment( $ment_ID, ARRAY_A );
$post = &get_post( $mentdata['ment_post_ID'] );
wp_notify_postauthor( $ment_ID, $mentdata['ment_type'] );
break;
default:
echo "error";
}
exit;
}
}
add_action( 'ment_post', 'ajaxify_ments', 20, 2 );
And here is my script:
jQuery('document').ready(function($){
var mentform=$('#mentform'); // find the ment form
mentform.prepend('<div id="ment-status" ></div>'); // add info panel before the form to provide feedback or errors
var statusdiv=$('#ment-status'); // define the infopanel
mentform.submit(function(){
//serialize and store form data in a variable
var formdata=mentform.serialize();
//Add a status message
statusdiv.html('<p>Processing...</p>');
//Extract action URL from mentform
var formurl=mentform.attr('action');
//Post Form with data
$.ajax({
type: 'post',
url: formurl,
data: formdata,
error: function(XMLHttpRequest, textStatus, errorThrown){
statusdiv.html('<p class="ajax-error" >You might have left one of the fields blank, or be posting too quickly</p>');
},
success: function(data, textStatus){
if(data=="success")
statusdiv.html('<p class="ajax-success" >Thanks for your ment. We appreciate your response.</p>');
else
statusdiv.html('<p class="ajax-error" >Please wait a while before posting your next ment</p>');
mentform.find('textarea[name=ment]').val('');
}
});
return false;
});
});
Every time I post a ment, I get: "Please wait a while before posting your next ment". Hoping somebody can tell me what I'm doing wrong?
I'm trying to 'ajaxify' menting in WordPress using this tutorial Ajaxify WordPress Comments
Here is my PHP handler:
function ajaxify_ments( $ment_ID, $ment_status ){
if( ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
//If AJAX Request Then
switch( $ment_status ) {
case '0':
//notify moderator of unapproved ment
wp_notify_moderator( $ment_ID );
case '1': //Approved ment
echo "success";
$mentdata = &get_ment( $ment_ID, ARRAY_A );
$post = &get_post( $mentdata['ment_post_ID'] );
wp_notify_postauthor( $ment_ID, $mentdata['ment_type'] );
break;
default:
echo "error";
}
exit;
}
}
add_action( 'ment_post', 'ajaxify_ments', 20, 2 );
And here is my script:
jQuery('document').ready(function($){
var mentform=$('#mentform'); // find the ment form
mentform.prepend('<div id="ment-status" ></div>'); // add info panel before the form to provide feedback or errors
var statusdiv=$('#ment-status'); // define the infopanel
mentform.submit(function(){
//serialize and store form data in a variable
var formdata=mentform.serialize();
//Add a status message
statusdiv.html('<p>Processing...</p>');
//Extract action URL from mentform
var formurl=mentform.attr('action');
//Post Form with data
$.ajax({
type: 'post',
url: formurl,
data: formdata,
error: function(XMLHttpRequest, textStatus, errorThrown){
statusdiv.html('<p class="ajax-error" >You might have left one of the fields blank, or be posting too quickly</p>');
},
success: function(data, textStatus){
if(data=="success")
statusdiv.html('<p class="ajax-success" >Thanks for your ment. We appreciate your response.</p>');
else
statusdiv.html('<p class="ajax-error" >Please wait a while before posting your next ment</p>');
mentform.find('textarea[name=ment]').val('');
}
});
return false;
});
});
Every time I post a ment, I get: "Please wait a while before posting your next ment". Hoping somebody can tell me what I'm doing wrong?
Share Improve this question asked Feb 24, 2014 at 12:42 henrywrighthenrywright 10.3k25 gold badges96 silver badges153 bronze badges 5-
Whats the value of data and textStatus when you try it? Put some
console.log(data); console.log(textStatus);
inside the success. – Gonz Commented Feb 24, 2014 at 12:55 - Just done that... The value of both console.log(data); and console.log(textStatus); is success – henrywright Commented Feb 24, 2014 at 13:05
- If so, it should go for the "Thanks for your ment. We appreciate your response." message! Do you see the ment in the database? – Gonz Commented Feb 24, 2014 at 13:09
- Yes! The ment is in the database. I agree, I should be seeing the "Thanks for your ment. We appreciate your response." message but for some reason I'm not. That's the problem... it's puzzling me – henrywright Commented Feb 24, 2014 at 13:13
-
what is
formurl
? what should it be? the tutorial doesn't explain – vsync Commented Apr 15, 2018 at 13:13
1 Answer
Reset to default 5Try this:
jQuery('document').ready(function($){
var mentform=$('#mentform'); // find the ment form
mentform.prepend('<div id="ment-status" ></div>'); // add info panel before the form to provide feedback or errors
var statusdiv=$('#ment-status'); // define the infopanel
mentform.submit(function(){
//serialize and store form data in a variable
var formdata=mentform.serialize();
//Add a status message
statusdiv.html('<p>Processing...</p>');
//Extract action URL from mentform
var formurl=mentform.attr('action');
//Post Form with data
$.ajax({
type: 'post',
url: formurl,
data: formdata,
error: function(XMLHttpRequest, textStatus, errorThrown)
{
statusdiv.html('<p class="ajax-error" >You might have left one of the fields blank, or be posting too quickly</p>');
},
success: function(data, textStatus){
if(data == "success" || textStatus == "success"){
statusdiv.html('<p class="ajax-success" >Thanks for your ment. We appreciate your response.</p>');
}else{
statusdiv.html('<p class="ajax-error" >Please wait a while before posting your next ment</p>');
mentform.find('textarea[name=ment]').val('');
}
}
});
return false;
});
});