Jump to content


Autofills add fields on dropdown change

form js

  • Please log in to reply
2 replies to this topic

#1 mavershim

mavershim

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

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.

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,095 posts
  • LocationLondon

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 ;)
Posted Image

#3 mavershim

mavershim

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

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.





Also tagged with one or more of these keywords: form, js

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users