CodeIgniter Forums
Cannot access request values from controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Cannot access request values from controller (/showthread.php?tid=86071)



Cannot access request values from controller - dziadek - 01-06-2023

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


RE: Cannot access request values from controller - dziadek - 01-06-2023

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


RE: Cannot access request values from controller - FramJamesgbc - 01-30-2023

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,


RE: Cannot access request values from controller - ikesela - 01-30-2023

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


RE: Cannot access request values from controller - GrehklkYonccd - 03-28-2023

Hi there, it looks like you're having trouble accessing the request data from your form. Without more information, it's difficult to pinpoint the exact issue, but one suggestion would be to try debugging the code and checking if the form data is being submitted correctly. You could also try using a different method of accessing the form data, such as $_POST['fname'], to see if that resolves the issue. Another possibility is that there may be an issue with your controller method or routing that is preventing the data from being properly accessed. It may be helpful to review your code and double-check that everything is set up correctly. If you continue to experience issues, feel free to provide additional details and we can try to provide further assistance.


RE: Cannot access request values from controller - InsiteFX - 03-28-2023

You also use your Web Browsers Developer Tools by hitting F12 like in Chrome Edge etc;