I’m new to Yii framework and am not advanced in PHP but I know what I’m doing as a beginner. I want to renderPartial view on another view and also have the results show after submit. I have two views namely: staff-index for staff (with model: staffinfo) and index for student (with model: studentlaptop). Currerntly, I did renderPartial of the index on top of staff-index and the form on index view appears but when I enter information on the form and submit, I don’t see the results. The page just refreshes. I want a user to fill out the form and show the result on the same view but for now I can’t do that.
Can you help me fix this issue, please?
Here’s my Controller
/* -- Student Laptop DB -- */
public function actionStudentlaptop()
{
$firstnames = false;
$model = new studentlaptop();
if (!empty($_POST)) {
$model->load($_POST);
if ($model->validate()) {
$firstnames = $model->findFirstnames();
}
}
return $this->render('index', ['model' => $model, 'firstnames' => $firstnames]);
}
/* -- Staff Laptop DB -- */
public function actionStaffinfo()
{
$firstname = false;
$model = new staffinfo();
if (!empty($_POST)) {
$model->load($_POST);
if ($model->validate()) {
$firstname = $model->findFirstname();
}
}
return $this->render('staff-index', ['model' => $model, 'firstname' => $firstname]);
}
There are more to this e.g the way you instantiate the models, the query object you defined in your previous Yii 1.1 thread (seemingly without performing the query).
I also recommend that you thoroughly read the guide.
I do refer to the docs but I get confused with what goes where with the examples given. Meaning, does the example code goes to controller, model or view. I’m still learning PHP and am new to MVC framework too! I just started on Yii last month, so there’s a lot I need to learn here.
Why the form on the partial render (index) inherits the form fields of the staff-index? I think that maybe the problem? It seems to me that the partial render does not call its model with the set rules, attributes and the findFirstnames function.
If mean you want to use information from the staff lookup for selecting student info, that’s a different topic. In this thread I believe you asked how to show info from the student model together with staff info (in the view you presented).
Yes, correct that I want to show data from student with staff info not the firs first sentence. But the partial rendered form did inherit the staff form fields. That’s why I was asking why I can’t see the results of the student model.
Do you mena you want to use the student model in the "staff form fields" in the index view? As a search form for student?
You can pass more than one model as params to the render call(s). Or a model instance as well as the array you return from the findFirstNames() call. Just give those items different names. If you need use two different models (excluded form in staff-index view?) you can handle both in the same controller action.
BTW The names (firstname/firstnames) are a bit misleading.
(I think you should consider adding a computed field to the model instead of building an array.)
I want to use two different models (excluded forms) on staff-index view. I don’t know how to handle those actions in controller. What is computed field and how can I add that?
I think code examples would help so much! I’m also reading the Yii2 By Example by Fabrizio and still trying to figure out what I’m doing.
From the first you provided the $user also refers to table name, right? That being the case, my sql query is too complicated with multiple CASES and using table views and that makes it hard for me to wrap my head around the example. Like this:
I’m not trying to solve your problem (which you btw didn’t explain in full).
I posted a couple of links to the guide, not a solution. You have to work out something by yourself or perhaps other forum users will make suggestions.
(I’m currently learning what differs in Yii2 and since I’m not programming/testing a project it will take some time. I might answer questions if I think I can add anything useful.)