Using jquery to reference php class function

Discussion in 'jQuery' started by patter, Jul 31, 2011.

  1. #1
    I'm very new to jquery and am playing around with it to try to better understand it. I ran across an example on the web that showed how to display something on a web page by calling a php file. This is that code:
    <script type="text/javascript">             
      $(document).ready(function(){   
        $("#position").click(function(){   
          $("#position span").load("test.php");   
        });   
      });         
    </script>  
    Code (markup):
    The above works fine but I need to be able to load a function that is in a php class. For example, given
    <?php
    class myclass {
    
      function myclass(){
      }
      
      function get_text() {
         return 'hello';
      }
    }
    
    PHP:
    How would I call the get_text function using the jquery code above (or whatever jquery code it would need)? The normal (php) method would be to use $myclass->get_text() but that doesn't work if I replace test.php with it.

    I've tried searching the web for this but the words class and function have meanings in jquery so the results weren't what I was looking. I would appreciate any help with this.
     
    patter, Jul 31, 2011 IP
  2. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #2
    usually done with ajax request 
    
    $.ajax({  url: 'test.php?action=gettext',  success: function(data) {
          alert(data);  }});
    PHP:
    and your test.php file :
    
    <?php
     if (!empty($_GET['action'])){
        if($_GET['action'] == "gettext"){
              echo "hello";
        }
    }
    ?>
    
    PHP:
    if everything goes correctly you should see a alert with hello that it got from the php script.

    good luck :)
     
    astrazone, Aug 1, 2011 IP