0

My problem is when I use ajax to POST, in Controller after I gather data in Model my next step is to access the View but unfortunately its not working, I only view it in inspect element in firefox. This is my sample code:

public function view_generate_jo_number()
    {
            $jo_num    =   $this->input->post('jo_num');
            $view_generated = $this->project_models->view_generate_jo_number($jo_num);
            $this->load->view('templates/header');
            $this->load->view('jo_generated_project_views', $view_generated);
            $this->load->view('templates/footer');
    }

So I think again for another solution which is I have this ajax..when in success it will redirect to another function within the same controller.

$("#view").click(function()
    {
        $.ajax({
            url : '<?php echo base_url('jo_generator/view_generate_jo_number')?>',
            type: 'POST',
            data: {
                    jo_num: $('#new_jo').text()
                  },
            success: function(data){
                $.ajax({
                url : '<?php echo base_url('jo_generator/view_jo_number')?>',
                success: function(){
                }
                });
            }
            });
    });

But my problem is in my first controller:

public function view_generate_jo_number()
    {
        if ($this->jo_generator_security_models->isAllowedToViewGenerateJO() === TRUE)
        {
            $jo_num    =   $this->input->post('jo_num');
            $view_generated = $this->project_models->view_generate_jo_number($jo_num);
        }
    }

I want to get the variable $view_generated so that I can use it in another function which is this:

public function view_jo_number($view_generated)
    {
            $this->load->view('templates/header');
            $this->load->view('jo_generated_project_views', $view_generated);
            $this->load->view('templates/footer');
    }

Because I need that variable to view the data in views.

Is there a way I can access it using success in ajax or I will call the variable from another function? Can someone help me with this?

user987654321
  • 119
  • 1
  • 1
  • 9

1 Answers1

1

You could return the variable and pass it to the success function:

public function view_generate_jo_number()
    {
        if ($this->jo_generator_security_models->isAllowedToViewGenerateJO() === TRUE)
        {
            $jo_num    =   $this->input->post('jo_num');
            $view_generated = $this->project_models->view_generate_jo_number($jo_num);

            return $view_generated;
        }
    }

-

$("#view").click(function()
    {
        $.ajax({
            url : '<?php echo base_url('jo_generator/view_generate_jo_number')?>',
            type: 'POST',
            data: {
                    jo_num: $('#new_jo').text()
                  },
            success: function(data){
                // use data for what ever you want. It should be the return value of view_generate_jo_number
                $.ajax({
                url : '<?php echo base_url('jo_generator/view_jo_number')?>',
                success: function(){
                }
                });
            }
            });
    });
Michael Brenndoerfer
  • 3,483
  • 2
  • 39
  • 50
  • When I alert the data it is empty. How can I use that return value in url in ajax so that I can access the variable like this: public function view_jo_number($view_generated) { } – user987654321 May 13 '14 at 08:44
  • First: data should not be empty: Have a look here: http://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success Second: I'm not sure how you call your php functions and what's underlying but you could try something like: `[...] jo_generator/view_jo_number(data)[...]` – Michael Brenndoerfer May 13 '14 at 08:54