DEV Community

Cover image for Voting System Javascript
Hasi
Hasi

Posted on

Voting System Javascript

Working on a project here at jhoom.xyz and have stuck at a place where I would like to limit voting per IP.
` function voteReview(reviewId, voteType) {
const data = {
action: 'ipf_vote_review',
reviewId: reviewId,
voteType: voteType,
postId: <?php echo $post->ID; ?>
};

        jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function(response) {
            if (response.success) {
                const reviewElement = jQuery('.single-review').eq(reviewId);
                if (voteType === 'up') {
                    let upCount = parseInt(reviewElement.find('.thumb:first').text().match(/\d+/)[0]) + 1;
                    reviewElement.find('.thumb:first').html('&#128077; (' + upCount + ')');
                } else {
                    let downCount = parseInt(reviewElement.find('.thumb:last').text().match(/\d+/)[0]) + 1;
                    reviewElement.find('.thumb:last').html('&#128078; (' + downCount + ')');
                }
            } else {
                alert(response.data);
            }
        });
    }
Enter fullscreen mode Exit fullscreen mode

`

Top comments (1)

Collapse
 
dennistobar profile image
Dennis Tobar

Hi Hasi,

About your question, seems to be validated in the backend, because the backend has the IP address in the PHP request when you send the vote using jQuery POST. To access to IP address you must use $_SERVER['REMOTE_ADDR']; to retrieve the remote address, but it may more complex than this variable (proxys and others), so you may use this answer from stackoverflow to retrieve the IP address.