0

I have a function in my controller:

function getAllExpense()
    { $date=$this->frenchToEnglish_date($this->input->post('date'));
      $id_user=$this->session->userdata('id_user');
      $where=array('date'=>$date, 'id_user'=>$id_user);
      $allExpenses=$this->expenses_model->get_all('depenses',$where);
      return json_encode($allExpenses);

    }

In the view, I need to do an ajax_call to getAllExpense, but I need also to retrieve the result returned by getAllExpense in case of success(and I can't do this). Here is my view

function searchExpense()
    {
      var date= $('#dateSearch').val();
      $.ajax({
                            type:'POST',
                            url : '<?php echo site_url()."/public/expenses/getAllExpense/"; ?>',
                            data :'date='+date,

                            success:function(data)
                            {          $('#searchExpense').toggle();


                            }

                    });     
    }

and finally the form :

<div id="searchExpense" class="toogle_form" style="display:none">
           <label><strong>Vos depenses :</strong></label>
           <?php
              if (isset($allExpenses) && ! empty($allExpenses))
                                            {
                          foreach ($allExpenses as $expense)
                                    {
                                      echo $expense['id_depense'] ;
                                      echo ' ';
                                      echo $expense['commentaire'];
                                      echo '</br>';
                                        }
                              }
                              else
                              {
                                echo 'Vous n\'avez pas eu de depenses pour cette date';
                              }
            ?>
       </div>

This does not show anything on the browser. I can't use the method of sending data by loading the view in getAllExpense because I already load it in the function index. Could anyone help me please?

2 Answers 2

3

In your controller function getAllExpense, try echoing your json data with a header rather than using return:

Change this:

return json_encode($allExpenses);

to:

$this->output->set_header('Content-Type: application/json; charset=utf-8');
echo json_encode($allExpenses);
Sign up to request clarification or add additional context in comments.

6 Comments

THank you for your answer but how can I get $allExpenses in the view?
You wont be able to pass the php variable into the view via JSON. you will have to use Javascript (and likely jQuery) to fill the values that you get back from the server.
I aleady use JQUery. But I can't get back values from the server. Here is my problem. How can I do it please ?
In your AJAX success function, try alerting data. If you made the initial change I mentioned, you should see the info in the data object
I used $.get to show data with an alert. But what I would like to do is to display this data on the browser(not with an alert window). I don't know if it is possible to pass this data to my searchExpense form (as it is written in my question)
|
1

The url:

Reading it backwards I understand getAllExpense to be your function name and expenses to be your controller name... What is /public/?

The data:

Currently you're using data :'date='+date. This is incorrect. $.ajax expects an object as the data parameter. Use data: {date: date}.

Where's the dataType?

If you're expecting json from the server, tell $.ajax this so it can parse the response properly. Use: dataType: 'json'

Your success function

It helps for debugging to call console.log() on the returned data. Inside the callback use: console.log(data); Then, open up your web tools. If you're using Chrome press ctrl+shift+j and inspect the console tab when firing your AJAX request. If you head over to the network tab and click on the most recent request (at the bottom) you can view the HTTP response as well.

"and finally the form :"

You didn't include a form.

Isolate the problem:

You're much better off inspecting the AJAX response using Chrome's Network tab as I've mentioned, but you could also isolate the issue to a client one or server one by changing your function temporarily to:

function getAllExpense(){
    //$date=$this->frenchToEnglish_date($this->input->post('date'));
    $date = "12/21/2012"; /*or whatever your date format is.*/
    $id_user=$this->session->userdata('id_user');
    $where=array('date'=>$date, 'id_user'=>$id_user);
    $allExpenses=$this->expenses_model->get_all('depenses',$where);
    //return json_encode($allExpenses);
    echo json_encode($allExpenses);
}

And calling getAllExpense directly in the URL bar. You'll be able to see the raw json and determine if it's in a format you're happy with.

2 Comments

thank you for your answer, but I have already checked the format of $allExpense and it is like what I want to have. My problem is with the view. I need to post the date to the server (getAllExpense function) and retrieve the result of this function. I tried with ajax calls, but I only managed to send the date or to retrieve data , not the two actions in the same time. public/expenses/getAllExpense is the whole path to my function (public is the folder where are my controllers, expenses is the name of my controller and getAllExpense is the name of the function).
Provide a sample of $allExpenses. What do you want the client to do once the server responds to it's AJAX request? currently you're only calling $('#searchExpense').toggle();... you probably want to use the data somewhere on your page? As I said, perform console.log(data) in the callback to inspect if it's being returned and then update your DOM.

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.