2

I have a ajax function written which is posting different numbers. Here is the ajax function.

self.giveCashtoChild = function(){
            $.ajax({
                type: 'POST',
                url: BASEURL + '/index.php/main/addUserChildrenCash'+"/"+self.selectedchild(),
                contentType: 'application/json; charset=utf-8'
            })
            .done(function() {

            })
            .fail(function(xhr, status, error) {
                alert(status);
            })
            .always(function(data){                 
            });
        }

self.selectedchild() has the value of 2 , so Basically the url is addUserChildrenCash/2 but then it does not go to the codeigniter controller and change the page. Here is the controller function.

public function addUserChildrenCash($childID){
            if (!$this->session->userdata('user_id')){
                redirect('main'); // the user is not logged in, redirect them!
            }

                $userid= $this->session->userdata('user_id');
                $this->load->model('main_page');
                $childname =  $this->main_page->getChildName($childID, $userid);

                $data = array(
                    'name' => $childname['children_name']
                );
                $this->load->view('header2_view');
                $this->load->view('add_user_children_cash_view' , $data);
                $this->load->view('footer_view');

        }
5
  • Are you using CodeIgniter's built-in CSRF protection? If so, then your ajax data must include the value of the hidden CSRF token field. This is the part that gets submitted with the standard form action but is missing when you do ajax. Commented Mar 25, 2016 at 14:26
  • @Sparky I am not sure what that is ? Commented Mar 25, 2016 at 14:31
  • Check for CSRF in your CodeIgniter configuration file. See: codeigniter.com/user_guide/general/… Commented Mar 25, 2016 at 14:32
  • How can I get rid of that ? $config['csrf_protection'] = FALSE; $config['csrf_token_name'] = 'csrf_test_name'; $config['csrf_cookie_name'] = 'csrf_cookie_name'; $config['csrf_expire'] = 7200; $config['csrf_regenerate'] = TRUE; $config['csrf_exclude_uris'] = array(); Commented Mar 25, 2016 at 14:34
  • Get rid of what? Looks like you already have it deactivated... $config['csrf_protection'] = FALSE Although I strongly suggest you learn about what it means and learn how to use it. Commented Mar 25, 2016 at 14:41

3 Answers 3

1

You define ajax as POST but you sending it via GET

 type: 'POST',
 url: BASEURL + '/index.php/main/addUserChildrenCash'+"/"+self.selectedchild(),

So your code should be

In Ajax

var id = self.selectedchild(); # Assign data to here
$.ajax(
    {

        type:"post",
        url: "<?php echo base_url(); ?>index.php/main/addUserChildrenCash", 
        data:{ id:id},
        success:function()
        {

        }
        error:function()
        {

        }
        always:function(){

        }
    });

In controller

public function addUserChildrenCash()
{
    $id = $this->input->post("id");
    echo $id
}
Sign up to request clarification or add additional context in comments.

5 Comments

Tried it , not sure why its not working, I do get all variables.
console.log(id) and check console
did you change base url to this <?php echo base_url(); ?>??
console.log(id) value comes to 2 and yes i did that second one as well
oohh Happy face at end :)
0
self.giveCashtoChild = function(){
            var id = self.selectedchild();
            $.ajax({
                type: 'POST',
                url: BASEURL + '/index.php/main/addUserChildrenCash/"+id,
                contentType: 'application/json; charset=utf-8'
            })
            .done(function() {

            })
            .fail(function(xhr, status, error) {
                alert(status);
            })
            .always(function(data){                 
            });
        }

2 Comments

please use an alert(id); after var id = self.selectedchild(); to see if it has the correct value
Yup has the right value of 2, but its not sending it to the controller
0

In your codeigniter use case I would pass your ajax parameter like this:

 $.ajax({
   type: 'post',
   url: BASEURL +'/index.php/main/addUserChildrenCash',
   data: { 'childID': self.selectedchild() },
   })
  ...

In the codeigniter controller I would receive the parameter like this:

public function addUserChildrenCash() {
    $childID = $this->input->post("childID");
    ...
    }

You should also check wether your BASEURL has the correct value assigned. Hope this helps.

1 Comment

why have CHildID inside public function addUserChildrenCash() ?? and yea did not work :(

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.