1

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 enter image description here

7
  • Do you want to pass in the URL or as a POST value? Commented Mar 15, 2019 at 11:11
  • Sir, I want to pass two variable in Ajax URL like url: "<?php echo base_url(); ?>Admission/fetchStudent/"+ xclass, Commented Mar 15, 2019 at 11:16
  • u are only sending 1 param data: {'xclass': xclass} and u are not using this value in your controller as well, i think u are using slug from the url. Commented Mar 15, 2019 at 11:16
  • Sir I had also use {'xclass':xclass, 'section':section} but i failed Commented Mar 15, 2019 at 11:19
  • try with {xclass:xclass, section:section} Commented Mar 15, 2019 at 11:21

2 Answers 2

3

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.
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

$_POST('xclass') this will create problem i think, check other answer.

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.