• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ajax success is not working in codeigniter

I need to pass some values from my controller to view.
Here is my controller. 

public function my_function()
                echo 1;
            include APPPATH .'class/MyClass.php';

Whenever I add the [b]include APPPATH .'class/MyClass.php';[/b] to my controller, the echo is not working, but when I remove that the echo is working and gives me an alert with "1" in my view.

Here is my ajax in view

            url: "<?php echo base_url('index.php/home/my_function');?>",
            type: "POST",
            data:  new FormData(this),
            contentType: false,
            cache: true,
           success: function(data)

What does MyClass have to do with anything? and why would you try to "include" it?
It sounds like my_function() is a method inside your Home controller, in which case the AJAX reference would work.
Better would be to use "site_url('home/my_function');", as that will add the index.php if needed.

include APPPATH . 'class/MyClass.php';
$networkOnlineObject=new MyClass($networkOnlineArray);
$requestParameter = $networkOnlineObject->NeoPostData;
Actually I need to pass "$requestParameter" to the view. So that I can't avoide the that function

Hmm - that's not the CI4 way! You should have to "include" anything!
It looks like you are mixing traditional PHP with CI4 Undecided

So what should I do, How can I access the class anyway. I am new to CI.

Can't tell wihtout knowing more about your app, and I don't have time for that now, sorry.
I trust you hace gone through the CI4 tutorial and introduciton sections in the user guide? They try to explain how to use CI4.

I still use CI 3 not 4 yet, however let me give you a working example of Ajax which might help you. 

The example below is about "updating some informations of a company" 


<script type="text/javascript">


                    var company_id = $('#company_id').val();
                    var company_name = $('#company_name').val();
                    var company_about = $('#company_about').val();

                      url : "<?php echo base_url('YourController/function_name_please');?>",
                      method : "POST",
                      data : {company_id:company_id,company_name:company_name, company_about:company_about},
                setTimeout(function(){// wait for 0,2 secs 
               location.reload(); // then reload the page.
      }, 200);

          error: function(data){
              console.log('There is a mistake somewhere');
                    }); // ajax ends here


Controller : 

PHP Code:
public function function_name_please(){

$company_id $this->input->post('company_id');
$company_name $this->input->post('company_name');
$company_about $this->input->post('company_about');

$data = array(
  'company_name' => $company_name,
  'company_about' => $company_about



Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.