Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to implement star rating with Jquery, Ajax

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 303
    Comment on it

    Hello Friends,

    If you are looking to implement star rating with Ajax. Please find the below example for the same:

    1) Open your HTML file and write the code below to display stars: NOTE :: I have attached the primary key with div-id(tutorial-id; ?>)

    1. <table class="demo-table">
    2. <td style="width:12%" valign="top" align="center">
    3. <div id="tutorial-<?php echo $results[$i]->id; ?>">
    4. <input type="hidden" name="rating" id="rating" value="<?php echo $results[$i]->rating; ?>" />
    5. <ul onMouseOut="resetRating(<?php echo $results[$i]->id; ?>);">
    6. <?php

    // Display 5 stars with the below code for($j=1;$j<=5;$j++) {

    1. $selected = "";
    2. if(!empty($results[$i]->rating) && $j<=$results[$i]->rating) {
    3. $selected = "selected";
    4. }
    5. ?>
    6. <li class='<?php echo $selected; ?>' onmouseover="highlightStar(this,<?php echo $results[$i]->id; ?>);" onmouseout="removeHighlight(<?php echo $results[$i]->id; ?>);" onClick="addRating(this,<?php echo $results[$i]->id; ?>);">&#9733;</li>
    7. <?php } ?>
    8. <ul>
    9. </div>
    10. </td>
    11. </table>

    2) Place the below code in your Jquery part ::

    1. <script type="text/javascript">
    2.  
    3. function highlightStar(obj,id) {
    4. removeHighlight(id);
    5. $('.demo-table #tutorial-'+id+' li').each(function(index) {
    6. $(this).addClass('highlight');
    7. if(index == $('.demo-table #tutorial-'+id+' li').index(obj)) {
    8. return false;
    9. }
    10. });
    11. }
    12.  
    13. function removeHighlight(id) {
    14. $('.demo-table #tutorial-'+id+' li').removeClass('selected');
    15. $('.demo-table #tutorial-'+id+' li').removeClass('highlight');
    16. }
    17.  
    18. function addRating(obj,id) {
    19. $('.demo-table #tutorial-'+id+' li').each(function(index) {
    20. $(this).addClass('selected');
    21. $('#tutorial-'+id+' #rating').val((index+1));
    22. if(index == $('.demo-table #tutorial-'+id+' li').index(obj)) {
    23. return false;
    24. }
    25. });
    26.  
    27. $.ajax({
    28. type: "POST",
    29. url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
    30. data: { 'action':'custom_add_rating', 'id':id, 'rating': $('#tutorial-'+id+' #rating').val() },
    31. });
    32. }
    33.  
    34. function resetRating(id) {
    35. if($('#tutorial-'+id+' #rating').val() != 0) {
    36. $('.demo-table #tutorial-'+id+' li').each(function(index) {
    37. $(this).addClass('selected');
    38. if((index+1) == $('#tutorial-'+id+' #rating').val()) {
    39. return false;
    40. }
    41. });
    42. }
    43. }
    44. </script>

    3) Open your PHP FILE where you would like to call ajax to update records in database and make the below code:

    function custom_add_rating_callback(){ global $wpdb;

    1. $rating = array('rating' => $_POST["rating"]);
    2. echo $wpdb->update('wp_connections', $rating, array('id' => $_POST["id"]));
    3. echo $wpdb->last_query;
    4. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: