Cannot access request values from controller |
Hello guys, I stuck with accessing my request.
Here's from: <form action="http://****/ajax/" method="POST"> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname"><br> <label for="lname">Last name:</label><br> <input type="text" id="lname" name="lname"> <input id="send" type="submit" value="send"> </form> Controller method: public function ajaxTest() { $fname = $this->request->getVar('fname'); print($fname); }
Btw if I make form action='baseurl/public/test.php'
I can print_r($_POST) without any problems
It requires a bit of understanding of Codeigniter 4 new structure. But you will love it! CodeIgniter 4 rocks with it’s rewritten framework. I really enjoy learning the framework, even though it takes a lot of time,
Your need to set 3 part:
Routes (App\Config\Routes.php) : wil be like this: http:yourdomain.com/ajax $routes->get('ajax', 'Home::viewForm', ['as' => 'ajaxform']); // to view form , need create function viewForm in controller (this example) , route with name $routes->post('ajax', 'Home::ajaxTest'); Your FORM: (app\Views\ your_view_file_names.php): <form action="<?=route_to('ajaxform')?>" method="POST"> Your Controller Home.php (App\Controller): public function viewForm() { return view('App\Views\your_view_file_names' , [ 'data' => $your_data_to_show_in_view ]); } public function ajaxTest() { $fname= $this->request->getPost('fname'); $lname = $this->request->getPost('lname'); $allPost = $this->request->getPost(); // get all value return redirect()->back(); } |
Welcome Guest, Not a member yet? Register Sign In |