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

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


Code:
public function my_function()
             {
                $data['name']=$name=$this->input->post('name');
                $data['email']=$email=$this->input->post('email');
                $data['phone']=$phone=$this->input->post('phone');
                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


Code:
$.ajax({
            url: "<?php echo base_url('index.php/home/my_function');?>",
            type: "POST",
            dataType:'json',
            data:  new FormData(this),
            contentType: false,
            cache: true,
            processData:false,
           success: function(data)
             {   
                alert(data);
             }
          });
Reply

#2
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.
Reply

#3
include APPPATH . 'class/MyClass.php';
$networkOnlineArray['Field_Existence_Indicator_Transaction']['amount']=$amount;
$networkOnlineArray['Field_Existence_Indicator_Billing']['billToFirstName']=$name;
$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
Reply

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

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

#6
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.
Reply

#7
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" 

AJAX : 

Code:
<script type="text/javascript">
                $(document).ready(function(){
                  $('#send-button').click(function(e){

                    e.preventDefault;

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

                    $.ajax({
                      url : "<?php echo base_url('YourController/function_name_please');?>",
                      method : "POST",
                      data : {company_id:company_id,company_name:company_name, company_about:company_about},
                      success:function(data)
                    {
                       
                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

                  });
                });
              </script>

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
);

$this->load->model('Backend_model');
$this->Backend_model->lets_update_it($data$company_id);

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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