-1

I am working on a new project and it's on a nice framework which is CodeIgniter, but I got some problem when I want to use ajax (I am very bad with this). I want to get the value of a select on my controller but I don't know why (maybe because I am bad with ajax) I can't get the correct value. When I want to know what the ajax request send me it's show a boolean.

On the network app from Chrome I got the request who send the correct value which is "annee : the value of the select" but can't get it on my controller..

This is my ajax :

$('#annee').change(function(){
    var annee = $('#annee').val();
    console.log(annee);

    $.ajax({
        url: "admin",
        type: 'post',
        data: {annee:annee},
    })
});

This is my Model (cmip_model.php):

function getWhere($annee) {

    $query = $this->db->get_where('cmip_surcharges_go', array('annee' => 
$annee));
    return $query->result ();
}

This is my controller (CMIP.php):

public function admin()
{
    $this->load->model('cmip_model');
    $cmip_Obj = new cmip_model();
   
    $surcharges = $cmip_Obj->getWhere($this->input->post('annee'));

    $data = array();
    $data['surcharges'] = $surcharges;

    $data['page_title'] = " :: Admin";
    $data['main_content'] = 'client/CMIP/admin';
    $data['active'] = 'CMIP';
    $this->load->view('_includes/template', $data);
}

And this is my view :

  année: <select id="annee">
        <option value="">--année--</option>
        <option value="2019">2019</option>
        <option value="2020">2020</option>
        <option value="2021">2021</option>
        <option value="2022">2022</option>
        <option value="2023">2023</option>
        <option value="2024">2024</option>
    </select>
 <?php var_dump($this->input->post('annee'));
 var_dump($surcharges);
 ?>

Actually I got this from my var dump :

B:\wamp\www\cotationtro\applications\frontoffice\views\client\CMIP\admin.php:380: boolean false

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Can you clarify: You are examining the output of `input->post('annee')); var_dump($surcharges); ?>` by looking at the Network tab of the Chrome Developer Tools, correct? – Quentin May 09 '19 at 09:03
  • Oh no, i saw the answer directly on my page! Only looking the request of the ajax on the network tab :) – martin genot May 09 '19 at 09:05
  • Based on information in comments on my (deleted because it was completely wrong because of misleading information elsewhere) answer, this is probably a duplicate of https://stackoverflow.com/questions/4197976/codeigniter-disallowed-key-characters – Quentin May 09 '19 at 09:35
  • It's weird because the answerare just number like 2019, 2020 but i will try thanks ! – martin genot May 09 '19 at 09:39
  • i dont understand the flow here. you have a page (i guess it is not above), that makes an ajax call to the function admin and that renders a view as an ajax response? what are you trying to accomplish. step by step. – Alex May 10 '19 at 04:00
  • Hey @Alex, i want to send the – martin genot May 10 '19 at 07:11
  • @martingenot by "insert" i guess you don't mean in the database but rather use as a variable. i would open dev tools and go to the network pane and look for your request. see if it generates a 404 then check the answer below. further i'm not really understanding why you are rendering a view in your ajax method. what are you planning on doing with this as i can see you don't have any logic to use the html response in your js/ajax code. – Alex May 10 '19 at 07:26

1 Answers1

0

Make changes in Ajax URL

url: "<?= base_url('controller_name/function_name')?>",
Danish Ali
  • 2,354
  • 3
  • 15
  • 26
  • "Also, use single ' or double quotes " to declare a variable in Ajax call" — That's a property name, not a variable, and using an identifier is fine. There are no special characters in the name `annee` that would require representing it as a string in that context. – Quentin May 09 '19 at 09:09
  • @Quentin Got it. – Danish Ali May 09 '19 at 09:10
  • @Alex according to the question I post an answer. But not due to use appropriate wording (`"Also, use single ' or double quotes " to declare a variable in Ajax call"`) answer got downvoted instead of edit. – Danish Ali May 10 '19 at 04:41