Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8947811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:51:45+00:00 2026-06-15T12:51:45+00:00

I have a PHP file, …/datastuff.php On this page a PHP object is instantiated.

  • 0

I have a PHP file, …/datastuff.php
On this page a PHP object is instantiated. (The class is written in another file).

public class MyClass{

        //....
       public function doAfterClick(){
        //.....
       }
}

So in datastuff.php I do

$MyObject = new MyClass();
 //user specific data is added to $MyObject

Now I want to call $MyObject->doAfterClick(), when the user presses a button on datastuff.php

I think AJAX is the general solution for this type of problem. But I am not sure how to do this?
If I wanted to call a PHP function written on another page I could use AJAX to POST data to that page. But I want to stay on this page, because MyObject needs to call the method.

If I POST back to datastuff.php won’t $MyObject be lost?
If I make it a Singleton or Global would this then work?
I looked at this, which is a similar problem, ut is not quite what I am looking for:
Using JQuery ajax to call a PHP file, process, but stay on same page

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:51:46+00:00Added an answer on June 15, 2026 at 12:51 pm

    Here’s the basic idea:

    # datastuff.php
    
    // This object will be created upon each request
    $MyObject = new MyClass();
    
    $action = (array_key_exists('action', $_GET)) ? $_GET['action'] : '';
    switch ($action) {
        case 'do_something':
            $response = $MyObject->doAfterClick();
            // Output relevant feedback to the user or redirect them somewhere based on the response
    
            // This was most likely called via AJAX, so perhaps output JSON data or
            // some other means of communicating the response back to the javascript
            break;
    
        case 'do_something_else':
            // etc...
            break;
    
        default:
    
            // Present the information to the user...
    
            break;
    }
    

    Your $MyObject will be created upon each request (i.e. on initial page load and after the button is clicked and fires an AJAX request) but it may be identical in each request assuming it has been given the same data. After the button is clicked, an ajax request to “datastuff.php?action=do_something” will make sure the relevant method is called.

    Although something like this would work, it is not good code design. I’d recommend you spend some time researching MVC (Model, View, Controller) patterns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP File with something like this: <script type=text/javascript> var page =
I have a php file which starts a session, this is followed by some
I have a php-file which includes another html-file: <? virtual(../top.html);?> The problem is that
I have php file with a class in it that needs to read a
I have php file which contains class FooClass uses in a few projects. I
The situation is next: I have php file, which parses a web-page. on that
I have a PHP file that create an array of class with array_push and
I have a json.php file which delivers results like this: { markers: [ {'a1_id':4213CK58,
I have php file, in this file I have this code: <script language=JavaScript type=text/javascript
I have this problem where all URLs like context/path/file/anything/that/follows becomes context/path/file.php/anything/that/follows or context/path/file.xml/anything/that/follows .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.