1

I'm working with Codeigniter.

I need to access twice to a php function, but the first time I don't have to echo a json_encode array....I should echo it the second time when I call it from a js file. How do I do it?

So, php file:

function filter(){
    if (function call is not from js file ) {
      //using $_POST variable to get info from the form
      dont echo json
    }elseif (function call is from the js file) {
      echo json_encode($data); 
     //but $data should be $_POST info too, but in this case $_POST WOULD 
     //BE EMPTY and I'm sending no data to ajax
    }
}

from js file:

$.ajax({        
    type: "GET",
    url: "filter",       
    dataType: 'json',

   success: function(data){
     alert(data); },

   error: function(data){
     alert('error'); }
5
  • Why do you want to call the same function twice? Commented Jun 30, 2015 at 15:58
  • A quick hack would be adding a parameter data: in your ajax and store a variable in there. you can then check the variable on the server side. More: stackoverflow.com/questions/4406348/… Commented Jun 30, 2015 at 15:58
  • @light bc I'm handling a form (to filter information) and if certain info is required then I have to keep filtering Commented Jun 30, 2015 at 18:04
  • @DipenShah I think I have too much info in order to send it again with a post :s I know this can be a solution, but is there any other way to do what I want without this logic? Commented Jun 30, 2015 at 18:28
  • @Limon Then I think cillosis's solution hits the nail. You need to check if the request is an XMLHTTPRequest. While this works you might wanna read this stackoverflow.com/questions/18260537/… Commented Jun 30, 2015 at 18:35

1 Answer 1

1

You can do some searching a find a lot of different options for detecting an AJAX request with PHP. For example:

Once you make sure it is from AJAX, you can pass a parameter like pass and assign it a 1 or 2 depending on which pass. Still very hacky though.

The real issue is that this function is doing too much. You should have two different functions if they do different things. When you start introducing conditions like this, you violate the Single Responsibility Principle (part of SOLID) and are asking for issues in the future.

But, for completeness, you can do something like this and it should in most cases detect the AJAX request:

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    // This is PROBABLY an AJAX request
    if ($_POST['pass'] == 1) {
        // First Pass
    } else if ($_POST['pass'] == 2) {
        // Second pass
    }
}

NOTE: This can be manipulated by the client, so don't rely on it for driving anything requiring security. I'm not advocating for this method by the way -- you really should separate concerns.

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

4 Comments

the problem I have is that when doing the ajax call to the php function I don't have loaded the $_POST variable (bc it would be the 2nd time that it enters the funciton)... so I won't be able to get the info once came in the first POST. It's a filtering function...how can I do it?
If you must have state transfer between requests, the standard mechanism is to use Sessions.
@cillosis I did what you recommend, I used sessions. Also I have splitted the functions. Now the ajax function makes the get call over another php function. But I will accept your answer because that was my original question and it works.

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.