Welcome Guest, Not a member yet? Register   Sign In
Access values based on dropdown value.
#1

Hello,

I have students database from which I want to access students data based on class section, when any section is selected from dropdown list.

For I tried below:

my jquery is below, where sections is my dropdown id.

Code:
$("#sections").change(function(){
      var sectionid = $(this).val();
      $.ajax({
        url: "<?php echo $baseurl; ?>/Students/getStudents",
        type: "POST",
        data:{sectionid:sectionid},
        success:function(data)
        {
      // $("#session").find('option').not(':first').remove();
         // $("#sections").html(data);
        }
      });
    });

getStudents function in controller Students is below:

PHP Code:
public function getStudents()
    {
        $model =new StudentModel();
        if($this->request->getVar('sectionid'))
        {
            $id $this->request->getVar('sectionid');
          return $model->getStudents($id);   
        
}      
    


and the  getStudents function with param function in StudentModel is:

PHP Code:
public function getStudents($id)
    {
       $sectionid $id;
       $sql "SELECT s.*, e.* FROM tblstudents s INNER JOIN tblenrollment e ON s.id = e.studentid WHERE e.sectionid=?";
       $query $this->db->query($sql,$sectionid);
       $results $query->getResult();
       return $results;
    


now my problem is to retrieve these values in my view

I tried like below, but it requires parameter, please help.

PHP Code:
$data['students'] = $model->getStudents(); 
Reply
#2

(This post was last modified: 11-12-2020, 02:06 PM by schertt.)

If you're retrieving that data from the Model using AJAX, then by that point you've already given the View (and all included data) to the client; you can't add further data to the View as you would prior to that point.

To do something with the data that AJAX call returns, you instead need to actively modify the DOM using plain JS, JQuery, etc. inside the AJAX call's success callback to change the page into what you want to display.

If you instead want that data passed into the View prior to returning it to the client, your onChange callback doesn't need to make an AJAX call for data but instead needs to load a new page entirely, where your Controller function then gets that data in the same manner and includes it in the returned View.

It's also good practice to define the AJAX call's dataType parameter to ensure you (and JQuery) know what you're truly dealing with.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB