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'); }
Limon
  • 1,652
  • 6
  • 27
  • 55
  • Why do you want to call the same function twice? – light Jun 30 '15 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: http://stackoverflow.com/questions/4406348/how-to-add-data-via-ajax-serialize-extra-data-like-this – Dipen Shah Jun 30 '15 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 – Limon Jun 30 '15 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? – Limon Jun 30 '15 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 http://stackoverflow.com/questions/18260537/how-to-check-if-the-request-is-an-ajax-request-with-php – Dipen Shah Jun 30 '15 at 18:35

1 Answers1

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.

Community
  • 1
  • 1
Jeremy Harris
  • 23,007
  • 13
  • 73
  • 124
  • 1
    +1 for the note. More http://stackoverflow.com/questions/18260537/how-to-check-if-the-request-is-an-ajax-request-with-php – Dipen Shah Jun 30 '15 at 18:36
  • 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? – Limon Jul 06 '15 at 19:55
  • If you *must* have state transfer between requests, the standard mechanism is to use Sessions. – Jeremy Harris Jul 06 '15 at 19:57
  • @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. – Limon Jul 07 '15 at 15:49