插入AJAX/javascript值到自定义的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>';
            }

好了,我所要做的就是插入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!");
    }); 

最新更新