CodeIgniter Forums

Full Version: Database operation In Jquery
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Rahul gamit[/eluser]
Hi,
I am using select dropdown html control and one textbox,
I have used onchange() event of select dropdown control to fill the text box value,

this is the code
Code:
<select id="cmb_fruit" name="cmb_fruit">
<option value="1">Apple</option>
<option value="2">Mango</option>
<option value="3">Orange</option>

&lt;input type="text" id="txt_fruit" name="txt_fruit"/&gt;

[removed][removed]

[removed]
$(function() {
   $('#cmb_fruit').change( function() {
     &lt;?php
      
      ?&gt;
       $('#txt_fruit').val($(this).find('option:selected').text());
      
   });
});
[removed]

so this code will fill the value whenever the value of the dropdown is changed,
And i want to perform database operation whenever the data is changing ,
for example: if i select fruit i am having the value apple i want to select data from the database, so how can i perform codeigniter database operation in jquery?

thanks in advance .

El Forum

[eluser]pickupman[/eluser]
I will generally make a AJAX handler in my controller. Using jQuery inside of your change event use post to post the values to your controller to update the db.

Code:
//jQuery
$("#cmb_fruit").change(function(){
  var form = $("form[name='your_form_name']").serialize();
  $.post('&lt;?php echo site_url('controller/jquery_handler');?&gt;',form, function(data){
    //maybe do something with json here
    $('#txt_fruit').val($(this).find('option:selected').text());
  });
});

//Controller/handler
function jquery_handler(){
  $this->load->library('form_validation');
  $this->form_validation->set_rules('cmb_fruit', 'fruit', 'required|trim');

   if( $this->form_validation->run() ){
      $data['fruit'] = $this->input->post('cmb_fruit');
      //Do a query here
   }
}