I have a custom form. The values are sent via ajax to the action function in the controller.
(This is in essence what i’m doing: Implementing Ajax in Block View Templates)
This function retrieves a json file from another server. I would like to put this into a variable and use it in the view. Like this $this->set(‘data’, $data); But in the view the variable is empty.
$b = $this->getBlockObject();
$bv = new BlockView($b);
What is the best solution here?
Are you using the form helper (eg $form->text(…) etc.) to render your form view, or is it just html?
Its just plain html, no form helper
When a form is rendered again, the core form helper re-fills any posted values (for example, in a core dashboard dialogue when you make a mistake).
With just plain html, you will need to re-populate fields from posted values by yourself.
Ok thanks. But the way to go is to render the block view again in the action function?
There are many ways to do it.
- As you are, rendering the whole block and replacing. You will need to re-fill form values from post data either in your code or using the form helper. (The form helper can be useful, but it also has some annoying habits you may bump into and need to work round).
- Capturing form content before AJAX in your JS and subsequently re-populating form content from your JS after rendering the whole block and replacing.
- Leaving the form alone and only returning the new info.
- (There are no-doubt other variations)
Which is best depends on your personal preference and the overall situation. In various situations I have used all of the above.
The settings Export/Import system I provide in edit mode for many of my addons uses a variation of (1). In addition to (1), for Export/Import, my Form Reform addon uses (3) for AJAX submission of front end forms with a custom extension of the core form helper.
That makes the options clear. I think, i’m the closest to option 2 now but prefer option 3, will investigate that. Thanks a lot.
I found out that its possible to trigger an action function from another block. In my case the (search) form keeps its values and only the results which are rendered by another block are updated via ajax.