How do I get a page to refresh with ajax

Discussion in 'jQuery' started by lunkan87, Mar 7, 2014.

  1. #1
    Hi

    I trying to get a page to refresh without using reload. Im using a widget called raty and I need to refresh the page everytime someone rate an image. How can I make it refresh so the current votes and average shows in the divs?

    Here is the rating jquery plugin code

    //Raty plugin
    $(function() {
    //Below is a path to img folder.
          $.fn.raty.defaults.path = 'js/img'; 
    //Below will activate Raty on div where class is star.     
          $('.star').raty({
        half  : true,
            number: 5,
            score : 0,
            click: function(score, evt) {
    //Below will get id value and store in variable pid               
            var pid=$(this).prop('id');
    //Below will post score and id value to raty1.php page behind the scenes.   
                $.post('raty1.php',{score:score, pid:pid},
                        function(data){
                   
                          
                });
              }
         });
    });
    Code (markup):
    the votes and average numbers shows in these divs
    <div class="votes"><?php echo "Antal röster: " . count($names).'<br>';?></div>
    <div class="average"><?php echo "Genomsnitt: " . $formatted;?></div>
    Code (markup):

     
    lunkan87, Mar 7, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Just update the divs with content received from the ajax (post) function. Calculate the average and any other numbers you need, and send it back from the PHP-file.
    No need to update the page, just replace the content in the divs
     
    PoPSiCLe, Mar 7, 2014 IP
  3. lunkan87

    lunkan87 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    I need to use ajax to refresh. Its for a course Im taking and we need to use ajax to refresh a page. And I dont know how to code that, I google it but found nothing that I understood.
     
    lunkan87, Mar 7, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    The whole point of AJAX is to obtain _parts of the code_ not the whole page. Ie, you reload parts of the content, or specific items in the code - hence why I said just load the content into the divs in question.
    Something like this:
    
              $.post('raty1.php',{score:score, pid:pid},
                        function(data){
                   var data = $.parseJSON(data); //assumes that you return data as JSON
                   $('.average').html(data.average); //assumes that one of the return values is named 'average' and contains the new average number
             });
      
    Code (markup):
     
    PoPSiCLe, Mar 7, 2014 IP