php - Auto complete fields of a previous values -
i doing small application in yii framework database this
=== invoices === id (pk) customer_id invoice_title order_no invoice_issue_date due_date description === customers === id (pk) email_address customer_name address city state postal_code description
i have rendered customer model
in invoice model
can enter values both models in single invoice form
.but there 1 problem,let assume have customer name xyz
had saved before
.now when going again fill customer name xyz
,it should show all fields of both models invoice_title,order_no,invoice_issue_date,due_date,description,email_address,customer_name,address etc. in input fields of form
don't have re-enter fields again
.so how can achive in yii framework.any , suggestions highly appreciable.more clarification on codes have done can shared if needed. please me out.i totally stuck here.
to has mentioned need ajax, , javascript. logic this:
when value selected in dropdown customer name, trigger ajax call retrieve information user. can done ajax option, available additional htmloption html element helpers in chtml, part of clientchange.
echo $form->dropdownlist($model,'customer_name',chtml::listdata(customers::model()->findall(),'id','customer_name'), array(// htmloptions 'ajax'=>array(// special htmloption through clientchange, ajax 'type'=>'get', 'url'=>$this->createurl('controllername/customerdetails'),// action generate data 'data'=>'js:"id="+$(this).val()',// data sending action in controller 'datatype'=>'json',// type of data expect server 'success'=>'js:updatefields'// javascript function execute when request completes ) ) );
the documentation above options ajax can seen in jquery's ajax documentation.
then in server side find particular customer, , send response browser. example:
// in controllername code action return values json public function actioncustomerdetails($id){ $var=customers::model()->findbypk($id); echo cjson::encode($var); }
when receive server response populate respective fields. can done in success function callback ajax, in above code updatefields:
yii::app()->clientscript->registerscript('update',' function updatefields(data, textstatus, jqxhr){ // select each input field id, , update value $("#customers_postal_code").val(data.postal_code); $("#customers_city").val(data.city); $("#customers_address").val(data.address); // update fields other inputs } ');
notes: customer can have many invoices, question invoice select given customer name. that's you'll have handle, think answer has enough code going.
to know input field ids, can check generated html.
Comments
Post a Comment