php - Second select depending on first select using AJAX in CodeIgniter -
i want second selects options vary depending on choice in first select. if pick "skalman" can't pick "whole day lunch" , if choose "lilleskutt" can't choose "half day". haven't worked ajax before thinking might right way go, if me out more grateful!
i options database, using 2 functions in booking.php:
private function get_room_options() { $this->load->model('booking_model'); $rooms = $this->booking_model->get(); $rooms_form_options = array(); // room title , store in array foreach ($rooms $id => $room) { $rooms_form_options[$id] = $room->title; } return array( 'rooms_form_options' => $rooms_form_options, ); } public function get_package_options() { $room = $this->input->post('searchdata'); $this->load->model('package_model'); $packages = $this->package_model->get(); $packages_form_options = array(); foreach ($packages $id => $package) { $packages_form_options[$id] = $package->package_name; } return array( 'packages_form_options' => $packages_form_options, ); }
and in booking.php find form:
<?php echo form_open('booking/preview') ?> <div> <?php echo form_label('conference room: ', 'id') ; ?> <?php echo form_dropdown('id', $rooms_form_options, set_value('id')); ?> </div> <div> <?php echo form_label('package type: ', 'package_id') ; ?> <?php echo form_dropdown('package_id', $packages_form_options, set_value('package_id')); ?> </div> <div> <?php echo form_label('number of participants: ', 'number_people') ; ?> <?php echo form_input('number_people', set_value('number_people')) ; ?> </div> <div> <?php echo form_submit('preview', 'book'); ?> </div> <?php echo form_close(); ?>
and script far:
<script type="text/javascript"> $(function() { $(document).on('change', '[name=id]', function(e){ var searchdata = $(this).val(); $.ajax({ url: "<?php echo base_url('index.php/booking/get_package_options'); ?>", type: 'post', data: searchdata, success: function() { // how should options in here? } }); }); }); </script>
codeigniter doesn't support ajax.for more clarification http://phpframeworks.com/
Comments
Post a Comment