1

I'm trying to pass a Wordpress comment form, posted on the FrontPage, via Ajax. On submission, the success code of the JS is executed, but the comment is not inserted in the database. Thank you in advance for any help!

The HTML

    <form id="commentform" type="post" class="commentform">
    <label for="comment"></label>
    <textarea id="comment" class="comment" name="comment" aria-    required="true"></textarea>
    <input name="comment_post_ID" value="210” id="comment_post_ID" class="comment_post_ID" type="hidden"/>
    <input name="comment_parent" id="comment_parent" class="comment_parent" value="0” type="hidden"/>
    <input type="hidden" name="action" value="trytosendit"/>
    <input type="submit">  
    </form>

ajaxtry2.JS

    jQuery(document).ready(function(e) {

e('.commentform').submit(ajaxSubmit);

function ajaxSubmit() {
var comment_parent=e(".comment_parent").val();
var comment_post_ID=e(".comment_post_ID").val();
var comment=e(".comment").val();
jQuery.ajax({
  action:  'trytosendit',
  type:    "POST",
  url:     commentsent.ajaxurl,
  data:    {comment_parent:comment_parent,comment_post_ID:comment_post_ID,comment:comment},
  success: function(data) {
  alert(comment_parent + comment + comment_post_ID + 'This is data returned from the server ');
  }
});
return false;
  }});

Functions.php

    wp_enqueue_script( 'ajaxtry2', get_template_directory_uri() . '/JS/ajaxtry2.js', '', '', true );
    wp_localize_script( 'ajaxtry2', 'commentsent', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));

function trytosendit() {
global $wpdb;
$comment_parent = $_POST['comment_parent'];
$comment_post_ID = $_POST['comment_post_ID'];
$comment = $_POST['comment'];
$time = current_time('mysql');

$data = array(
'comment_post_ID' => $comment_post_ID,
'comment_author' => 'admin',
'comment_author_url' => '',
'comment_content' => $comment,
'comment_type' => '',
'comment_parent' => $comment_parent,
'user_id' => 1,
'comment_author_IP' => '',
'comment_agent' => '',
'comment_date' => $time,
'comment_approved' => 1,
);

wp_insert_comment($data);


wp_die();}
add_action( 'wp_ajax_trytosendit', 'trytosendit' );
add_action( 'wp_ajax_nopriv_trytosendit', 'trytosendit' );
2
  • your success message do not indicate actual success, just that something was received back (maybe not even that, maybe just that there was an http code 200 in the response). You should probably change your code to generate actual success values. apart from that, doesn't seems that there is obvious error in inserting the comment, you should check you error log and check what the wp_insert_comment returns Jun 14 '17 at 10:24
  • Thanks for the quick reply Mark. I have not found any way to crack this nut, so I'm officially giving up on this one ;)
    – Alexander
    Jun 14 '17 at 13:19
0

It took a while, but this works (a comment is created, albeit with still some missing values, but fixing that part is easy) Biggest changes: For some reason an 'echo' in functions.php was important. Also, I changed the 'action' in ajaxtry2.js under data:

HTML (values are fictional)

    <form id="commentform" class="commentform">
    <label for="comment"></label>
    <textarea id="comment" class="comment" name="comment"></textarea>
    <input name="comment_post_ID" value="1" id="comment_post_ID" class="comment_post_ID" type="hidden"/>
    <input name="comment_parent" id="comment_parent" class="comment_parent" value="1" type="hidden"/>

    <input type="submit"> 
    </form>

ajaxtry2.JS

    jQuery(document).ready(function(e) {

    e('.commentform').on('submit', (ajaxSubmit));

     function ajaxSubmit() {
     var comment_parent=e(".comment_parent").val();
     var comment_post_ID=e(".comment_post_ID").val();
     var comment=e(".comment").val();
     var BookingForm = jQuery(this).serialize();

jQuery.ajax({
  type:    'POST',
  url:     commentsent.ajaxurl,
  data:    {BookingForm:BookingForm,action:'trytosenditr'},
  success:function(msg){
  alert(msg +comment);
  }
});
return false;
  }});

functions.php

    function all_the_scripts2() {        
    wp_enqueue_script( 'ajaxtry2', get_template_directory_uri() . '/JS/ajaxtry2.js', '', array('jquery'), false );
    wp_localize_script( 'ajaxtry2', 'commentsent', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
    }
     add_action( 'wp_enqueue_scripts', 'all_the_scripts2' );

    function trytosendit() {
    global $wpdb;   
    parse_str($_POST["BookingForm"], $output);
    $comment_parent = $output['comment_parent'];
    $comment_post_ID = $output['comment_post_ID'];
    $comment = $output['comment'];
    $time = current_time('mysql');
    $current_user = wp_get_current_user();

    $data = array(
    'comment_post_ID' => $comment_post_ID,
    'comment_content' => $comment,
    'comment_parent' => $comment_parent,
    'comment_date' => $time,
    'comment_approved' => 0,
    'user_id' => $current_user->ID,
    );

    wp_insert_comment($data);
    echo 'hello';
    wp_die();
    }

Big fan of this Website, it has helped me a lot many times over!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.