2

I'm writing a function that will return database results.

I need it to return the data if the function is called from another function and echo the data when its called via AJAX.

Possible Solutions

  • Pass in a parameter...if($echo) echo $data; else return $data;
  • Separate functions...function employees_ajax() { echo $this->employees(); }

I'd prefer something smarter that can detect that 'itself' was called from another function and return instead of echo.

UPDATE

Another possibility would be to check the $_SERVER variable.

"As far as I know, the X-Requested-With is sent by the Ajax functions of most major Frameworks but not all...it's safe to say it's not generally a 100% reliable method to determine whether a request is an AJAX request or not."

Does $_SERVER['HTTP_X_REQUESTED_WITH'] exist in PHP or not?

It would be fail-safe to rely on the server rather than the client / js framework.

UPDATE 2: RESULT

function employees($id = NULL, $return = FALSE) {

    .
    .
    .
    if($return === TRUE) return json_encode($data);
    else echo json_encode($data);
}
Community
  • 1
  • 1
jared_flack
  • 1,606
  • 2
  • 17
  • 24
  • 3
    See here: http://stackoverflow.com/questions/3124636/detect-ajax-calling-url – Matt Jun 11 '12 at 15:06
  • I don't think it is a good approach. You should process the return on the calling php. – Sebas Jun 11 '12 at 15:18
  • What kind of data does `employees()` return? Are you wanting to return JSON if it's AJAX and a PHP array otherwise? – woz Jun 11 '12 at 17:19
  • @woz `employees()` should return a JSON string for both scenarios...`return json_encode($data);` or `echo json_encode($data);`. – jared_flack Jun 11 '12 at 17:32
  • What do you do with the JSON when the function is called by PHP? – woz Jun 11 '12 at 17:33
  • @woz It bootstraps the data: `` http://backbonejs.org/#FAQ-bootstrap – jared_flack Jun 11 '12 at 18:10
  • When would you use `return` inside `employees()` then? If you call a function via AJAX and do a `return`, you won't get the JSON data. – woz Jun 11 '12 at 18:19
  • @woz Yes. When my other PHP function calls `employees()` it should `return` the JSON string to bootstrap it into the page. When the AJAX calls `employees()` it needs to `echo` the JSON string (Note paragraph 2 of the original question above). So I need to find a way for `employees()` to distinguish between AJAX calls and calls from other functions. – jared_flack Jun 11 '12 at 18:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12407/discussion-between-woz-and-flackend) – woz Jun 11 '12 at 19:09

2 Answers2

2

Of course, "A PHP Error was encountered" error will be thrown, because, you are getting undefined key in $_SERVER superglobal.

First of all, create function that determine if query was from XMLHttpRequest.

/**
 * 
 * @return BOOl, true if ajax, false if regular way
 */
function is_ajax(){
  return isset($_SERVER['HTTP_X_REQUESTED_WITH']);
}

Then, depending on is_ajax() result, you should implement another function that represent key => val pairs from your SQL server.

Yang
  • 8,580
  • 8
  • 33
  • 58
1

Try giving your employees function an optional flag:

function employees ($shouldEchoResult = true) {

  if ($shouldEchoResult) {
    echo $result;
  }
  else {
    return $result;
  }

}

If it's called with AJAX, don't set the flag. Otherwise, in your code, use categories(false). That's the only other solution I can think of. PHP doesn't have anything built in for that like Ruby does.

woz
  • 10,888
  • 3
  • 34
  • 64