How to adapt jQuery load to use multiple variables?

Apologies for the unclear title. I was unsure how to phrase the question.

I have a table which displays my highscore. I retrieve the highscore through this:

function topScore() {
      $( '#tableid tbody' ).load( 'm1.php' );
}

This is linked to a php file (m1.php):

<?php

   $con = mysqli_connect('localhost','XX','XX','XX');

   if (!$con) {
   die('Could not connect: ' . mysqli_error($con));
   } else {

   $sql="SELECT name, bet FROM m1 ORDER BY bet DESC LIMIT 5";
   $result = mysqli_query($con,$sql);

   while($row = mysqli_fetch_array($result)) {
      $name = $row['name'];
      $bet = $row['bet']; 

      echo "<tr><td>$name</td><td>CC$bet</td></tr>"; 

       }
   }
   mysqli_close($con);  

?>

This grabs the top 5 from a table named m1, which holds high scores.

I need to adapt the jQuery load so that it can load the high scores from different tables, depending on a variable, and without creating a separate high score php file for every table. I currently have a variable which varies from m1-m5. How can I rewrite it so, depending on the value of this variable, it grabs the high scores from the table named that variable? If I was sending information, I was just pass over a string... but this is a load, so I'm not sure how I'd go about doing this.

I apologise if anything I've said seems ridiculous. I'm very new to all of this.

Answers 1

  • Use json in other words send the whole $result as its by

    echo json_encode($results);
    

    Using ajax to get the data to javascript

    $.ajax{
         url: "m1.php",
         type:  post,
         data: $.param({
                 mode: "data"//send this as post in other words on php file (url) just $_POST["mode"] = "data";
         }),
         dataType: "json",
         success : function(data){
                  //use the object data
          }
    }
    

    This is unclear you get the idea.use ajax to get load of data form php in addition the page does not reload.


Related Articles