Home > Software engineering >  changing select box on another select box value not working in codeigniter
changing select box on another select box value not working in codeigniter

Time:10-01

i have a codeigniter website, where there is a form for user to select category and subcategory, when a user selects category the subcategory dropdown should show accordingly, i did the following code:

public function subcategories() {
$category = $this->input->post('category');
$query = $this->db->query('SELECT name FROM subcategory WHERE parentcategory='.$category);
$data['subcategories'] = $query->result_array();
$this->load->view('homecontroller/subcategories', $data);
echo $category;
}

in subcategories.php

<?php foreach ($subcategories as $c): ?>
    <option value="<?php echo $c['name'] ?>"><?php echo $c['name'] ?></option>
<?php endforeach; ?>

and in my view:

$(document).ready(function() {
  $('#sl_countries').change(function() {
    $('#sl_cities').load("<?php echo site_url('index.php/homecontroller/subcategories') ?>", {
      category: $(this).val()
    });
  });
});
<div class="form-group col-md-6">
  <label for="inputEmail4">Product Category</label>
  <select id="sl_countries" class="form-control" name="cname" aria-label="Default select example">
    <?php
 foreach($listcategory as $val){
  echo '<option value="'.$val->name.'">'.$val->name.'</option>';
 }?>
  </select>
</div>
<div class="form-group col-md-6">
  <label for="inputEmail4">Sub Category</label>
  <select class="form-control" name="sname" id="sl_cities"></select>
</div>

however this is not working, i get an error like below when i checked the console:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 SELECT name FROM subcategory WHERE parentcategory=

can anyone please tell me what is wrong here, thanks in advance

CodePudding user response:

use this way:-

jQuery Ajax code :-

<script>
$(document).ready(function(){
 $('#country').change(function(){
  var country_id = $('#country').val();
  if(country_id != '')
  {
   $.ajax({
    url:"<?php echo base_url(); ?>dynamic_dependent/fetch_state",
    method:"POST",
    data:{country_id:country_id},
    success:function(data)
    {
     $('#state').html(data);
     $('#city').html('<option value="">Select City</option>');
    }
   });
  }
  else
  {
   $('#state').html('<option value="">Select State</option>');
   $('#city').html('<option value="">Select City</option>');
  }
 });
 
  });
  </script>

Controller Code :-

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dynamic_dependent extends CI_Controller {
 
function fetch_state()
 {
  if($this->input->post('country_id'))
  {
   echo $this->dynamic_dependent_model->fetch_state($this->input->post('country_id'));
  }
 }
 }
 ?>

Model Code :-

<?php
class Dynamic_dependent_model extends CI_Model
{

 function fetch_state($country_id)
 {
  $this->db->where('country_id', $country_id);
  $this->db->order_by('state_name', 'ASC');
  $query = $this->db->get('state');
  $output = '<option value="">Select State</option>';
  foreach($query->result() as $row)
  {
   $output .= '<option value="'.$row->state_id.'">'.$row->state_name.'</option>';
  }
  return $output;
 }
 }
 ?>

View Code :-

<div class="form-group">
   <select name="country" id="country" class="form-control input-lg">
    <option value="">Select Country</option>
    <?php
    foreach($country as $row)
    {
     echo '<option value="'.$row->country_id.'">'.$row->country_name.'</option>';
    }
    ?>
   </select>
  </div>
  <br />
  <div class="form-group">
   <select name="state" id="state" class="form-control input-lg">
    <option value="">Select State</option>
   </select>
  </div>
  • Related