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 5982467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:05:29+00:00 2026-05-22T22:05:29+00:00

What is the standard practise with PHP pages that are used for Ajax requests?

  • 0

What is the standard practise with PHP pages that are used for Ajax requests? Should they print out a single value (ex: get points of player with id = x)? Does/should a single page serve multiple requests? If so, how can code be grouped on the PHP side?

P.S: An additional question: If a templating system like Smarty is used, would it be more interesting security-wise to call the PHP page being used instead of calling the request page directly?

  • 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-22T22:05:30+00:00Added an answer on May 22, 2026 at 10:05 pm

    I use this piece of code in Javascript. Backend wise things are organized in a MVC type of organisation, so things affecting one module are usually grouped together. In general I also create a sperate module for a seperate model, but in some cases you may deviate from this principle.

    PHP

    Execute a piece of code and wrap it inside a try/catch block. This way error messages may be propagated to the frontend. This method helps in that regard to convert exceptions to a readable error. (to debug from json).

    try {
        //... execute code ..  go about your buisness..
        $this->result = "Moved  " . count($files) . " files ";
        // result can be anything that can be serialized by json_encode()
    } catch (Exception $e) {
       $this->error = $e->getMessage() . ' l: '  . $e->getLine() . ' f:' . $e->getFile();
       // return an error message if there is an exception. Also throw exceptions yourself to make your life easier.
    }
    // json response basically does something like echo json_encode(array("error" => $this->error, "result" => $this->result))
    return $this->jsonResponse();
    

    For error handling I often use this to parse errors.

    public function parseException($e) {
        $result = 'Exception: "';
        $result .= $e->getMessage();
        $trace = $e->getTrace();
        foreach (range(0, 10) as $i) {
            $result .= '" @ ';
            if (!isset($trace[$i])) {
                break;
            }
            if (isset($trace[$i]['class'])) {
                $result .= $trace[$i]['class'];
                $result .= '->';
            }
            $result .= $trace[$i]['function'];
            $result .= '(); ';
            $result .= $e->getFile() . ':' . $e->getLine() . "\n\n";
        }
    
        return $result;
    }
    

    Javascript side

    /**
     * doRequest in an ajax development tool to quickly execute data posts.
     * @requires jQuery.log
     * @param action (string): url for the action to be called. in config.action the prefix for the url can be set
     * @param data (object): data to be send. eg. {'id':5, 'attr':'value'}
     * @param successCallback (function): callback function to be executed when response is success
     * @param errorCallback (function): callback function to be executed when response is success
     */
    jQuery.doRequest = function (action, data, successCallback, errorCallback) {
        if (typeof(successCallback) == "undefined") {
            successCallback = function(){};
        } 
        if (typeof(errorCallback) == "undefined") {
            errorCallback = function(data ){
                alert(data.error);
            };
        }
        jQuery.log(action);
    
        jQuery.post(action, data, function (data, status)
        {
    
            jQuery.log(data);
            jQuery.log(status);
            if (data.error !== null || status != 'success') {
                // error handler
                errorCallback(data);
            } else {
                successCallback(data);
            }
        },'json');
    };
    

    Note: the error callbacks are very nice if you combine them with something like pNotify

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

Sidebar

Related Questions

I'm using HTML Purifier , a PHP filter that guards against XSS and ensures
I have a main class that configures and fires up Jetty. (That's standard practice
I have a function, foo(), that allocates memory and returns it. Is it standard
Is there a way to tell PHP how to convert your objects to ints?
I'm creating a simple application using the Kohana PHP framework, just FYI. This is
I'm developing a java web app on my local PC that uses a file
My program has two forms which get data from the same data source. After
I'm currently trying to write some Javascript to loop through the elements in a
Is there a universal way we can retrieve CellID in J2ME or what's the
Forgive my probably incorrect application of terminology in this question (btw, please correct me

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.