ajax and php class

how to access a php class using ajax

Page 1 of 1

2 Replies - 1569 Views - Last Post: 17 July 2010 - 05:22 PM

#1 sattua  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 83
  • Joined: 20-March 08

ajax and php class

Posted 16 July 2010 - 10:21 PM

Hi i have some knowledge working with ajax, but i don't know how to access a php class, I search on google but i did not find something useful. I trying to do something like this but using a php class.

function ajaxload(){
                var ajax=newAjax();//some other function without importance...
                ajax.open("GET", "myPHPclass.php?x=whatever",true);      
                ajax.onreadystatechange=function() {
                        if (ajax.readyState==4) {
                                var res = ajax.responseText
                                alert(res);
                        }
                }
                ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                ajax.send("");//here if i use post...
            }



So, in this case i working with simple php code, but this doesn't work with a php class, which is the way to do it?

thanks in advance...

Is This A Good Question/Topic? 0
  • +

Replies To: ajax and php class

#2 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: ajax and php class

Posted 17 July 2010 - 01:15 AM

Well what you've got there will call a file called myPHPclass.php and set $_GET['x'] to "whatever"

so, in myPHPclass.php :
echo "this text will appear in your alert box";
echo "by the way, the value of x is {$_GET['x']}!"; 



Does that help?

As far as it being a php class, well, you can't call that directly. But there's nothing to stop you having a class in your php file and calling it like so:
class thing
{
    public function __construct($var)
    {
       echo "an instance of {__CLASS__} is running, constructed with the parameter '{$var}'";
    }
}


thingthing = new thing($_GET['x']);



HTH.

This post has been edited by moopet: 17 July 2010 - 01:16 AM

Was This Post Helpful? 0
  • +
  • -

#3 sattua  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 83
  • Joined: 20-March 08

Re: ajax and php class

Posted 17 July 2010 - 05:22 PM

thanks for your help =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1