⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Autofills add fields on dropdown change



mavershim
  • profile picture
  • Member

Posted 23 April 2012 - 03:59 AM

Hi,

Is there a way to add a functionality that will autofill some fields on a form on dropdown change.
I have a customer table and a service request table, I have set a relation between this 2 table. But I want is to get the data from the customer table so if I select on a dropdown list of customer. on change it will autofill the address and other fields.

BTW grocery CRUD is the best CRUD i have seen so far.

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 23 April 2012 - 06:04 AM

This you can do it with javascript and jquery with a custom view:
so for example you can have:


$('input[name=customer_name]').change(function(){
$.ajax({
url: 'whatever',
dataType:'json',
success: function(result){
$('input[name=address]').val(result.address);
$('input[name=postcode]').val(result.postcode);
}
});
});


Ok I just write this script on my mind but you get the point ;)

mavershim
  • profile picture
  • Member

Posted 23 April 2012 - 09:11 AM

Thanks Johnny. Can this do? I'm just new to programming and I'm not that good in jQuery/javascript..
Can I just echo this on my controller before going to the form.
...
$output=$crud->render();
echo "$('input[name=customer_name]').change(function(){
$.ajax({
url: 'example/getAddress/'.$('input[name=customer_name]'),
dataType:'json',
success: function(result){
$('input[name=address]').val(result.address);
$('input[name=postcode]').val(result.postcode);
}
});
});";
$this->load->view('example.php',$output);
}
and how can I past the input to the another url to get the the values.

on another page:

function getAddress($where){
$query=$this->db->query('Select address,zipcode from Customer where
customerid =' .$where) ;
}


how to format the result to json so I can return it to other fields.