将AJAX / javascript值插入自定义WordPress表

时间:2016-11-10 13:24:19

标签: wordpress

这是我在网站上的投票功能。当用户单击按钮时,他们的投票将被插入到WordPress中的自定义表中。 AJAX返回成功消息OK,但没有任何内容插入到表中。我哪里弄错了?这是我的代码(AJAX脚本和PHP代码都在一页上)

    <script type="text/javascript" >
        jQuery(document).ready(function(){
                var counter;
                var id;
           jQuery('.fa-plus').one('click', function(){
                counter = 0;
                id = jQuery(this).closest('div').prop("id");
                alert(id);
                //window.open("bride-profile.php");
                counter = counter+1;
                jQuery('#votes-count').parent().html(counter);

        });



        jQuery.ajax({
            url    :"http://localhost/-wiz/wordpress/",
            method :"POST",
            data   :{
                        'action' : 'add_votes',
                        'counter': counter,
                        'id'     : id,
            },
            success:function(data){
                alert(data);            
            }
        }).error(function(){
            alert("ERROR!");
    });   
});
    </script> <?php
}//end of function my_action_javascript
function add_votes(){
        $id = $_POST['id'];
        $votes= $_POST['counter'];
        $wpdb->insert(
            'fwwp_votes',
            array(
                'bride_id' => $id,
                'votes'    => $votes
            )
        );
    }
add_action( 'wp_ajax_no_priv_add_votes', 'add_votes' );
add_action( 'wp_ajax_add_votes', 'add_votes' );

和我的一点加价:

foreach($applications as $application){
            $id = $application->id;
            echo       
            '<div class="col-md-3" id="'.$id.'">',
                    '<span class="pull-right votes" id="votes-count"><strong>0</strong></span>',
                    '<div class="disp" id="disp"><i class="fa fa-heart fa-fw"></i> Votes</div>',
                    '<i class="fa fa-plus fa-fw"></i> Vote for '.$application ->user_name.',
            '</div>';
            }

1 个答案:

答案 0 :(得分:0)

好的,所以事实证明我所要做的就是将我的jQuery.ajax块插入到onclick函数中,如下所示:

jQuery(document).ready(function(){
                jQuery('.site-footer').hide();
                var counter;
                var id;
        jQuery('.fa-plus').one('click', function(){
                counter = 70;
                id      = jQuery(this).closest('div').prop("id");
                alert(id);
                counter = counter+1;
                jQuery('#votes-count').parent().html(counter);


                jQuery.ajax({
            url    : "http://localhost/-wiz/wordpress/vote",
            type   : "POST",
            data   : {
                        'action' : 'add_votes',
                        'counter': counter,
                        'id'     : id
            },
            success:function(response){
                console.log(response);

            }
        }).error(function(){
            alert("ERROR!");
    }); 
相关问题