0

I'm trying to update my database data using checked box with ajax. the checkbox is running when I clicked it alert the success msg. but not passing the data to my controller. I don't know where to find my mistake here. I'm trying to figure out how to check if the data is pass to my controller.

Anyone can help me?

view

$checked = FALSE;
if($result->consignment == 1) { $checked = TRUE; }
    $data = array(
        'value'   => $result->id, 
        'onClick' => 'incoming_CheckboxState();',
        'class'   => 'incoming',
        'checked' => $checked
    );
echo form_checkbox($data);?> Incoming

ajax

function incoming_CheckboxState() {
  if ($('.incoming').is(':checked')) {

    var id = $('.incoming').val();
    var yes = 1;                
    $.ajax({
        type: "POST",
        url: "<?=based_url(); ?>user_accounts/update_permission",
        data: { user_id : id, status : yes },
        success: function(msg) {
            alert("Activated");
        }
    });
  } 
}

controller

public function update_permission(){
    $data = array(
       'id' => $this->input->post('user_id'),
        'status' => $this->input->post('status')
    );
    $this->user_accounts_model->incoming($data);
}

model

public function incoming($data){
    $this->db->set('consignment', $data['status'])
             ->where('id', $data['id'])
             ->update('users');
}

2 Answers 2

1

Do these

  1. Change AJAX URL

    url: "<?=base_url()?>user_accounts/update_permission"
    
  2. Set your base_url.(application/config/config.php)

    $config['base_url'] = 'http://stackoverflow.com/';
    
Sign up to request clarification or add additional context in comments.

10 Comments

this is my base_url = $config['base_url'] = '';
dont leave base URL empty. fill it
What's the effect if the based url is empty? I saw in tutorial they make it empty. and what URL should I put incase?
In CI3 URL is must, else you program can be hacked using URL Injection. Base url should be path to your root folder.(outside application folder..)
for example my main folder is ci_attl
|
0
url: "<?=based_url()?>./user_accounts/update_permission",

Should be (Your base url in config.php should be set with a trailing slash):

url: "<?= base_url(); ?>user_accounts/update_permission",

Where are you getting that . from?

Open your inspector, go to network and click on XHR to monitor ajax requests being made (so you can see if the request is made and if your post data is there as well).

3 Comments

it's still not working, in my network, i have form data user_id: 1 status: 1 ,,
http://[::1]/ci_attl/user_accounts/user_permission/%3C?=based_url();?%3E/user_accounts/update_permission this is the request URL
@GeninaAnneGabuten Seems your base url isn't set properly.

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.