![]() |
I recently migrated from CI 3 to 4 and I'm having a problem displaying $_POST values in my view. I have a filter in my view "listar.php" and when the form is sent the same view is loaded with new information based on the filter, but I cannot print the filter value when the page is loaded.
From what I noticed in CI 4 it is necessary to use "echo $request->getGet('search');" to print the value sent by the form, but the problem happens because I'm not loading "Config\Services::request()", if I put " $request = \Config\Services::request(); " at the beginning of my view the error "Undefined variable: request" disappears, but it is very exhausting to always have to put this line in all views, I would like to know if anyone has a suggestion on how to always leave Request() loaded in all views by default, I would appreciate it. Note: I haven't created the logic behind the filter yet, for now I just want to display what was sent in the filter itself My controller: PHP Code: namespace App\Controllers; Input I want to print the value: PHP Code: <input type="text" name="busca" id="busca" value="<?php echo $request->getGet('busca'); ?>" class="form-control">
What did you do in CI3?
and the code does not work in CI4?
(04-11-2024, 05:35 PM)kenjis Wrote: What did you do in CI3? Hello, in CI3 i would do the following: PHP Code: <input type="text" name="busca" id="busca" value="<?php echo $this->input->get('busca'); ?>"> that is, i would use "this->input->get('busca')" after submitting the page. I didn't participate in the creation of the project so i don't know where "$this->input->get()" came from, when i arrived at the company everyione used it like that. Today i was reading about the use of CI4 "->withInput()" and it's basically what i need, so i could use "echo old('busca')" in the view, but i don't know how to put withInput when loading the view. Use "$request = \config\services::request();" also solves my problem, but i don't know how to always load this service in all system views automatically, without having to declare it at the beginning of the file.
Oh, that code is the CI3 era code when MVC was not properly separated.
In CI4 there is no `$this->input` in the View. So it does not work. withInput() is used when you want to get variables after redirection. So it is not for your case.
This is a sample to pass Request to a view.
https://codeigniter4.github.io/CodeIgnit...parameters If you write code in BaseController, you can pass it to all views. PHP Code: <?php PHP Code: <?= esc($request->getGet('busca')) ?> By the way, if you have the following code, it has XSS vulnerability. Don't use code like this. You must escape all user inputs when you show them in HTML. PHP Code: <input type="text" name="busca" id="busca" value="<?php echo $this->input->get('busca'); ?>">
(04-12-2024, 05:46 PM)kenjis Wrote: Este é um exemplo para passar Request para uma view. Thank you very much, that was really what i needed. I also thank you for the tip on using "esc()", it's something i wasn't aware of, thank you very much. |
Welcome Guest, Not a member yet? Register Sign In |