0

I have some php codes, and there is a condition which declare type of ajax. Now I want to know, should I write all php codes for each request separately? In other word, should I write all php codes twice (almost repeatedly) for both methods?

if(!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest")
{
    // I'm ajax  
    $arr = array('key1'=>'value1', 'key2'=>'value2');
    echo json_encode($arr);

} else {

    // I'm not ajax
    $arr = array('key1'=>'value1', 'key2'=>'value2');
    $_SESSION["arr"] = arr;
    header('Location: '.$_SERVER['HTTP_REFERER']); // redirect to previous page
}

So, as you see, I have to write all PHP code twice. One time for regular request and one time for ajax request. In reality there is a lot of codes, Maybe 1000 lines of code that I have two write them again for ajax requests (while they are almost identical). Is this a normal way?

Also I want to know, is there any succinct approach? Actually I like to use a approach which needs to php code just one time for both requests ...!

4
  • No, you don't have to write all of your code twice. In general you would extract common functionality away from the UI-bound functionality. Then you would have two different UI components (one for AJAX/API requests, one for page/UI requests, since it doesn't make much sense to keep them on the same page) which would reference the common functionality. Commented Dec 12, 2015 at 13:56
  • @David Thanks for your comment. I understand what is your point almost. But actually still I can't use it ..! Is there any small example? I want to use it as my pattern .. Commented Dec 12, 2015 at 14:00
  • Another way of thinking about it - write the functions to extract the data to an array. Then write a couple of functions to format that data - one for HTML output and another one for AJAX. Commented Dec 12, 2015 at 14:29
  • @RyanVincent seems good. May you please write a answer under my question ? Commented Dec 12, 2015 at 15:03

1 Answer 1

1

I would create a class to handle those request and put common code right into a method used by both contexts:

// file: class.handler.php
class contextHandler() {
  public function handleHttp() {
    $this->handleGeneral();
    // What ever has to be done in this context
    $_SESSION["arr"] = arr;
    header('Location: '.$_SERVER['HTTP_REFERER']);
  }

  public function handleAjax() {
    $this->handleGeneral();
    // What ever has to be done in this context
    echo json_encode($arr);
  }

  private function handleGeneral() {
    // put common code here
    $arr = array('key1'=>'value1', 'key2'=>'value2');
  }
}

In your code you could then use that class:

include 'class.handler.php';
$handler = new contextHandler();
if(
  !empty($_SERVER["HTTP_X_REQUESTED_WITH"]) &&
  strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest"
)
{
  $handler->handleAjax();
} else {
  $handler->handleHttp();
}

This has of course to be adjusted to your concrete needs but offers a nice and clean way of reusing code and generating small and readable code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I got some idea from your answer. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.