www.webdeveloper.com
Results 1 to 4 of 4

Thread: Javascript that executes php class

  1. #1
    Join Date
    Aug 2007
    Posts
    58

    Javascript that executes php class

    What code do I need in my view()function that triggers the oopClasses.php page that executes the classes and function MapIt()?

    Code:
    function view(){
    	createXMLHttpRequest();
    	loadingMessage();
    	var url = "oopClasses.php?";
    	xmlHttp.onreadystatechange = handleStateChange;
    	xmlHttp.open("GET", url + new Date().getTime(), true);
    	xmlHttp.send(null);
    }
    PHP Code:
    <?php class Maps
    {
        function 
    viewmaps()
        {
            GLOBAL 
    $sybconnect;
            
    $sql="select distinct cellname,centname from celltech order by cellname";
            
    $link=$sybconnect->query($sql);
            
    $i=0;
            while(
    $row=$sybconnect->fetchArray($link)){
                
    $output[$i]['cellname']=$row['cellname'];
                
    $output[$i]['centname']=$row['centname'];
                
    $i++;
        }
        
    //$connect->close();
        
    return $output;
    }

        function 
    MapIt(){
            if(isset(
    $this)){
            
    //user wants to see the maps
            
    $maps = new Maps();
            
    $displaysite $maps->viewmaps();
            
            
    ?>
            <div style="height:400px;overflow:auto">
            <table>
                <tr>
                    <th>Cell Name</th>
                    <th>&nbsp;</th>
                    <th>Centname</th>
                </tr>
            <?php
            
    for($i=0;$i<count($displaysite);$i++){
                
    ?>
                <tr>
                    <td><strong><?php echo $displaysite[$i]['cellname'];?> (<a href="http://<?php echo $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];?>?option=cellname&task=editmap&cellname=<?php echo $displaysite[$i]['cellname'];?>">Edit</a>)</strong></td>
                    <td>maps to</td>
                    <td><strong><?php echo $displaysite[$i]['centname'];?></strong></td>
                </tr>
                <?php
            
    }
            
    ?>
            </table>
            </div><?php
              
    }
        }    
    ?>

  2. #2
    Join Date
    Mar 2008
    Posts
    123
    you can't directly, however you are calling the php file javascript, so just add the function at the end of your php file :

    PHP Code:
    <?php class Maps
    {
        function 
    viewmaps()
        {
            GLOBAL 
    $sybconnect;
            
    $sql="select distinct cellname,centname from celltech order by cellname";
            
    $link=$sybconnect->query($sql);
            
    $i=0;
            while(
    $row=$sybconnect->fetchArray($link)){
                
    $output[$i]['cellname']=$row['cellname'];
                
    $output[$i]['centname']=$row['centname'];
                
    $i++;
        }
        
    //$connect->close();
        
    return $output;
    }

        function 
    MapIt(){
            if(isset(
    $this)){
            
    //user wants to see the maps
            
    $maps = new Maps();
            
    $displaysite $maps->viewmaps();
            
            
    ?>
            <div style="height:400px;overflow:auto">
            <table>
                <tr>
                    <th>Cell Name</th>
                    <th>&nbsp;</th>
                    <th>Centname</th>
                </tr>
            <?php
            
    for($i=0;$i<count($displaysite);$i++){
                
    ?>
                <tr>
                    <td><strong><?php echo $displaysite[$i]['cellname'];?> (<a href="http://<?php echo $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];?>?option=cellname&task=editmap&cellname=<?php echo $displaysite[$i]['cellname'];?>">Edit</a>)</strong></td>
                    <td>maps to</td>
                    <td><strong><?php echo $displaysite[$i]['centname'];?></strong></td>
                </tr>
                <?php
            
    }
            
    ?>
            </table>
            </div><?php
              
    }
        }    
       
       
    MapIt();
     
    ?>

  3. #3
    Join Date
    Aug 2007
    Posts
    58
    Okay I made some changes to my javascript and also added some code to the beginning of my php. Can you tell me why I am getting this error (Call to a member function on a non-object)?

    Code:
    function view(){
    	createXMLHttpRequest();
    	loadingMessage();
    	var date = new Date();
    	var timestamp = date.getTime();
    	var poop = true;
    	var queryString = "poop=" + poop;
    	xmlHttp.onreadystatechange = handleStateChange; 
    	xmlHttp.open("GET","oopClasses.php?"+ queryString + "&time=" + timestamp,true);
    			xmlHttp.send(null); 
    }
    PHP Code:
    <?php
    if(isset($_GET['poop']) && $_GET['poop'] == 'true'){
        
        
    $test = new Maps();
        
    MapIt();
    }
    class 
    Maps
    {
        function 
    viewmaps()
        {
            GLOBAL 
    $sybconnect;
            
    $sql="select distinct cellname,centname from celltech order by cellname";
            
    $link=$sybconnect->query($sql);
            
    $i=0;
            while(
    $row=$sybconnect->fetchArray($link)){
                
    $output[$i]['cellname']=$row['cellname'];
                
    $output[$i]['centname']=$row['centname'];
                
    $i++;
        }
        
    //$connect->close();
        
    return $output;
    }

        function 
    MapIt(){
            if(isset(
    $this)){
            
    //user wants to see the maps
            
    $maps = new Maps();
            
    $displaysite $maps->viewmaps();
            
            
    ?>
            <div style="height:400px;overflow:auto">
            <table>
                <tr>
                    <th>Cell Name</th>
                    <th>&nbsp;</th>
                    <th>Centname</th>
                </tr>
            <?php
            
    for($i=0;$i<count($displaysite);$i++){
                
    ?>
                <tr>
                    <td><strong><?php echo $displaysite[$i]['cellname'];?> (<a href="http://<?php echo $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];?>?option=cellname&task=editmap&cellname=<?php echo $displaysite[$i]['cellname'];?>">Edit</a>)</strong></td>
                    <td>maps to</td>
                    <td><strong><?php echo $displaysite[$i]['centname'];?></strong></td>
                </tr>
                <?php
            
    }
            
    ?>
            </table>
            </div><?php
              
    }
              
        }    
        
    ?>

  4. #4
    Join Date
    Mar 2008
    Posts
    123
    you need to define the function/class first before calling it.

    Ex:

    PHP Code:
    function dog(){
       echo 
    'bone!';
    }

    $new dog(); 
    and not

    PHP Code:
    $new dog(); 

    function 
    dog(){
       echo 
    'bone!';


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles