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

  • Home
  • SEARCH
  • 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 3438480
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:11:34+00:00 2026-05-18T08:11:34+00:00

I’m struggling to design the server-side script that responds to requests from an Ajax

  • 0

I’m struggling to design the server-side script that responds to requests from an Ajax application.

In its current state, the app is divided into discrete pages (e.g., Orders, Items, Finances, etc.). Only when you switch between these pages does the actual webpage reload. Each page has it’s own “operator”, which is required into a root operator.php, to which all Ajax requests are directed.

Contained in each operator is this pattern:

foreach ($actions as $action) {
    switch ($action) {
    case 'get-items':
        [...]
        break;
    case 'get-item':
        [...]
        break;
    case 'update-item':
        [...]
        break;
    [...]
    }
}

The $actions are supplied by the request, e.g. operator.php?page=items&action=get-item&id=123.

As the application became more complicated, it helped to separate the logic of each action from the from context where the action was requested.

I found I would frequently use this pattern when I wanted to use an action’s logic internally in PHP:

$items = json_decode(file_get_contents('http://[...]/operator.php?action=get-items'));

(Needless to say, this design creates a lot of extra overhead.)

So instead I now have an Operator class that is extended by each page. I can create, for instance, an ItemsOperator, and directly call the action I want, without any encoding or decoding or superfluous HTTP requests:

$items = $itemsOperator->getItems();

I modified the script that responds to Ajax requests to use the Operator class, like so:

foreach ($actions as $action) {
    switch ($action) {
    case 'get-items':
        $json['items'] = $operator->getItems();
        break;
    [...]
    }
}

if (count($json)) {
    echo json_encode($json);
}

This approach works reasonably well, but I’ve never had any formal web development training, and I suspect there are established, better-abstracted patterns (which I maddeningly can’t find on Google). Numerous shortcomings with my home-brew approach inspired this question:

  1. The concept of the “Operator” class that contains “actions” is too vague and all-encompassing. How should I separate the logic?
  2. Where the approach gets especially messy is when I need to, for instance, search the items DB on the finances page. Do I also include the ItemsOperator alongside the FinancesOperator? (I currently duplicate the logic.)
  3. Is there a better way to interface the Ajax request script with the Operator objects (assuming I shouldn’t totally dump the concept of operators)? For instance, I currently have to write a script for each page that maps the URL “action” variable to the corresponding method of the operator object.

Very sorry if this question is too open-ended. I don’t have developers around me to bounce ideas off of (and like I said, I haven’t had any real training) — so it’s invaluable to hear the advice of the SO community. When designing a script like this one, the number of possibilities/approaches/strategies can be totally overwhelming. And once you’ve invested a couple days time in one particular approach, it can be very difficult to turn back.

Thanks very much for your consideration (and reading this far).

  • 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-05-18T08:11:35+00:00Added an answer on May 18, 2026 at 8:11 am

    Not a full answer, but I would separate operator.php into two files. One called operator.php, and the other operator_ajax.php. operator.php does what you have it doing now, but instead of echoing it, just puts into a variable so that it can be included. operator_ajax.php includes operator.php and echos the value. operator.php can then be included from any other file you wish (replacing the HTTP request).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.