I have two dependent dropdown box in a form. From them, I will use one dependent dropdown box's selected option to update a column's fields in a table in the DB. But, I really can't figure out, how exactly can I fetch the selected option from this dependent dropdown box and pass it to the database after I press the submit button of the form.
I am using ajax to do this task. I've followed some tutorials, but they only show, how to add options dynamically in a dependent dropdown box without any submit button. But, I need to pass a dependent dropdown option's value which I've selected from the dropdown box, to DB to update a column after pressing the submit button.
Here's my controller:
public function fetch_session() {
if($this->input->post('subject'))
{
echo $this->Result_Model->fetch_session($this->input->post('subject'));
}
if($this->input->post('syllabus'))
{
echo $this->Result_Model->fetch_syllabus($this->input->post('syllabus'));
}
}
Here's my Model:
public function fetch_session($subject) {
$this->db->select('session');
$this->db->group_by('session');
$this->db->from('student');
$this->db->where('subject',$subject);
$query = $this->db->get();
$output = '<option value="">Select Session</option>';
foreach ($query->result() as $row) {
$output .= '<option value="'.$row->subject.'">'.$row->session.'</option>';
}
return $output;
}
public function fetch_syllabus($syllabus) {
$this->db->select('profile');
$this->db->group_by('profile');
$this->db->from('syllabus');
$this->db->where('subject',$syllabus);
$query = $this->db->get();
$output = '<option value="">Select Syllabus</option>';
foreach ($query->result() as $row) {
$output .= '<option value="'.$row->subject.'">'.$row->profile.'</option>';
}
return $output;
}
Here's my View:
<body>
<div class = "container" align = "center">
<br><br>
<?php echo form_open('Result_Controller/fetch_session'); ?>
<label>Subject</label>
<select class="col-sm-2 custom-select custom-select-sm"
name="subjects" id="subjects">
<option value = "">Select Subject</option>
<option value = "CSE">CSE</option>
<option value = "Fisheries">Fisheries</option>
<option value = "Agriculture">Agriculture</option>
<option value = "NFS">NFS</option>
</select>
<br><br>
<label>Session</label>
<select class="col-sm-2 custom-select custom-select-sm" name="session" id="session">
<option value="">Select Session</option>
</select>
<br><br>
<label>Syllabus</label>
<select class="col-sm-2 custom-select custom-select-sm" name="syllabus" id="syllabus">
<option value="">Select Syllabus</option>
</select>
<br><br>
<input type="submit" value="Submit">
</form>
</div>
<script src = "<?php echo base_url('/js/jquery-3.js') ?>"></script>
<script src = "<?php echo base_url('bootstrap/js/bootstrap.min.js') ?>"></script>
</body>
</html>
<script>
$(document).ready(function() {
$('#subjects').change(function() {
var subject = $('#subjects').val();
if(subject != '')
{
$.ajax({
url:"<?php echo base_url();?>/Result_Controller/fetch_session",
method: "POST",
data:{subject:subject},
success:function(data)
{
$('#session').html(data);
}
});
}
if(subject != '')
{
$.ajax({
url:"<?php echo base_url();>/Result_Controller/fetch_session",
method: "POST",
data:{syllabus:subject},
success:function(data)
{
$('#syllabus').html(data);
}
});
}
});
});
</script>
I've done as far as I could understand and may be wasted my last 10 hours. I know it's an incomplete and even may be a wrong code . But I need to know, what I should do further. I'm working in CI for a while but I'm completely new in Ajax. So, if anyone thinks that, I may need to read some resource first, please share some. I've already read a bunch of articles regarding this problem but nothing has helped much till now.
Thanks in advance...