I am doing a small application in Yii Framework for that my database is something like 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 the Customer model
in Invoice model
so that I can enter all the values for both models in a single Invoice form
.But there is one problem,let us assume that I have a customer name xyz
which I had saved before
.Now when I am going to again fill the Customer name with xyz
,it should show all the fields of both models like invoice_title,order_no,invoice_issue_date,due_date,description,email_address,customer_name,address etc. in that input fields of the form
so that I don't have to re-enter all the fields again
.So how this can be achive in Yii framework.Any help and suggestions will be highly appreciable.More clarification on codes that I have done can be shared if needed.
Please help me out.I am totally stuck here.
You could do this in two stages, so that:
When the view is initially displayed, the customer is asked for their
email address
orcustomer_name
.The Controller Action that the form is submitted to then retrieves data from the Customer model for the submitted
email address
orcustomer_name
(I'll useemail_address
in my example below). Once retrieved, you can display your Single Invoice Form View with the data pre-populated for the customer if available.This concept could then be implemented as follows:
You could split that to two separate Controller Actions if you wish.
For Step 1 View, you could then have the following:
Step 2 view, you could then look like what you already have. Maybe something like:
To do this as everyone has already mentioned you need ajax, and some javascript. The logic is something like this:
When a value is selected in the dropdown for customer name, trigger an ajax call to retrieve the information about that user. This can be easily done with the ajax option, which is available as an additional htmlOption for some html element helpers in CHtml, as part of clientChange.
The documentation for the above options for ajax can be seen in jquery's ajax documentation.
Then in the server side find the particular customer, and send a response to the browser. Example:
When you receive the server response populate the respective fields. This can be done in the success function callback for ajax, in the above code it was updateFields:
Notes: Your customer can have many invoices, so the question will be which invoice to select given a customer name. That's something you'll have to handle, i think my answer has enough code to get you going.
To know the input field ids, you can simply check the generated html.
Have you checked the Yii Autocomplete widget? And you wouldn't have to worry about AJAX implementation. It does it for you.
Yii Framework: CJui AutoComplete
A more customized autocomplete solution in this link.
Yii Framework: custom-autocomplete-display-and-value-submission