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
#5

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.
Reply
#6

You also use your Web Browsers Developer Tools by hitting F12 like in Chrome Edge etc;
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB