Calling PHP function using ajax

Discussion in 'PHP' started by sw0rdz, Oct 8, 2009.

  1. #1
    In simple, I want to be able to call a function from a PHP object (already initiated). I don't want to run an entire PHP script, just one function of an object.

    I know how to do most of it. I can create the class, instantiate it, etc. I just want to know if and how ajax can call a certain method of an already instantiated object?

    My basic plan.

    1.) Get posted info.
    2.) Created objects and list.
    3.)use ajax to remove objects from the list
    ---The list is updated from the view of the user/visitor
     
    sw0rdz, Oct 8, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ajax is a new request, as such the object you "already instantiated" in your previous request (HTML page loading) is no longer existant. You will need to recreate it. (Unless you save it in the session, which I don't recommend)
     
    premiumscripts, Oct 8, 2009 IP
  3. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #3
    The problem is, as you have already been told. Your object no longer exists by the time when the user gets the HTML where AJAX will run. For that to happen your PHP will have returned the HTML content and died.

    You can:

    1. Get post information
    2. Create objects and list
    3. Store them into DB
    4. Return HTML+AJAX code
    5. Make request via AJAX for removing objects
    6. Attend AJAX request removing objects from DB

    Steps 1 and 5 are triggered by user.
     
    caprichoso, Oct 8, 2009 IP