AJAX评论表单在FrontPage上提交成功但未插入

时间:2017-06-14 作者:Alexander

我正试图通过Ajax将Wordpress评论表单发布在FrontPage上。提交时,将执行JS的成功代码,但注释不会插入数据库中。提前感谢您的帮助!

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;
  }});
功能。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\' );

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

虽然花了一段时间,但这起到了最大的变化(创建了注释,尽管仍有一些缺少的值,但修复该部分很容易):出于某种原因,函数中出现了“回声”。php很重要。此外,我还更改了ajaxtry2中的“操作”。数据下的js:

HTML(值是虚构的)

    <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;
  }});
功能。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();
    }
这个网站的忠实粉丝,它多次帮助了我!

结束

相关推荐

通过AJAX过滤自定义分类帖子

我一直在尝试通过点击一些图标来创建博客帖子的动态排序。帖子是自定义的帖子类型,并且具有自定义的分类法。The objective is:<单击(自定义分类法的)类别名称,在下面显示(附加)该类别的帖子。The problem is:WP\\u查询不接受参数success函数的结果是WP\\u查询参数,而不是html对象或js端可用的任何东西。The flow:我注册并本地化了脚本:wp_enqueue_script( \'ajax-pagination\', get_stylesheet_dire