Welcome Guest, Not a member yet? Register   Sign In
Cannot access request values from controller
#1

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);
    }
Reply
#2

Btw if I make form action='baseurl/public/test.php'
I can print_r($_POST) without any problems
Reply
#3

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,
Reply
#4

(This post was last modified: 01-30-2023, 08:30 AM by ikesela.)

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();
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB