简体   繁体   中英

How Can I pass two variable in Ajax URL to codeigniter controller?

I am working on a School Management system. I am just creating fetch student details by their class and section. I use ajax + codeigniter controller but I am unable to pass two variable in ajax call to perform 2 parameter search.

My Ajax Code

<script>
                $(document).ready(function () {
                    $('#example').DataTable({
                        'paging': true,
                        'searching': true,
                        'ordering': true,
                        'autoWidth': false
                    });
                    $('#student').click(function (event) {
                        event.preventDefault();
                        var xclass = $('.sclass').val();
                        var section = $('.section').val();

                        //
                        $.ajax({
                            url: "<?php echo base_url(); ?>Admission/fetchStudent/",
                            type: "POST",
                            data: {'xclass': xclass, 'section':section},
                            datatype: 'json',
                            success: function (data) {
                                $("#resultlist").html(data);
                            }
                        });
                    });
                }); //event.preventDefault(); 

            </script>

My Controller

public function fetchStudent($class,$section){
    $this->load->model('Admission_model');
    $data = $this->Admission_model->fetchStudentmodel($class,$section);
    echo '<pre>';
    print_r($data);
    exit();
    echo json_encode($data);
}

My Model is

public function fetchStudentmodel($x,$y) {
    $uid = $this->session->userdata('user_id');
    $data = $this->db->select('*')
            ->from('student')
            ->where(['user_id' => $uid,'class'=>$x, 'section'=>$y])
            ->get();

    if ($data->num_rows() > 0) {
        return $data->result();
    } else {
        return 'No data Available';
    }
}

See this image then you can understand what I want to do 在此处输入图片说明

In your case, you can pass 2 values in params as like:

data: { key1: value1, key2: value2 }

And you can get them in your controller by using $_POST

But in your example, you are sending only 1 param and not getting it in your controller, your are using your URL slug for getting class.

In your controller file, you can simple get the values in $_POST , you can check both values by using:

echo '<pre>';
print_r($_POST);

You will get your both values in $_POST then you can access by using $_POST['key1'] or $_POST['key2']

One more thing, i dont know why are you using single quote on your param's key, this will be converted in a string variable i think.

Second solution for your example is: var xclass = $('.sclass').val(); var section = $('.section').val();

data: 'class='+xclass+'&section='+section, // here you can use quote and & for separation.

Try This,

$.ajax({
      url: "<?php echo base_url(); ?>Admission/fetchStudent/",
      type: "POST",
      data: ({xclass: xclass, section:section}),//changes
      datatype: 'json',
      success: function (data) {
             $("#resultlist").html(data);
      }
 });
public function fetchStudent(){//changes
    $this->load->model('Admission_model');
    $class  = $this->input->post('xclass');//changes
    $section  = $this->input->post('section');//changes
    $data = $this->Admission_model->fetchStudentmodel($class,$section);
    echo '<pre>';
    print_r($data);
    exit();
    echo json_encode($data);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM