I need to pass HTML form fields using POST method to Slim framework in php. please help.
Asked
Active
Viewed 253 times
3 Answers
0
In index.php file
Initialize the Function and Route url
$app->post('/xyz/routename', 'functionname');
Create function
function functionname() { $app = \Slim\Slim::getInstance(); $credentials = $app->request->post(); }
All Input field value available in $credentials object.

Sujal Patel
- 2,444
- 1
- 19
- 38
-
Sujal Patel, I'll check it sir. – Raja Jun 20 '17 at 07:36
-
Hi Sujal Patel, Which file i have to Initialize the Function and Route url (that Form page or Slim page) . – Raja Jun 20 '17 at 07:39
-
main route file index.php – Sujal Patel Jun 20 '17 at 07:47
-
oh sir i'll try to implement. – Raja Jun 20 '17 at 07:51
-
not showing any error sir. Actually I need form fields pass to slim page and insert to Database please give and post example code. – Raja Jun 20 '17 at 07:56
0
You could do it like this:
$this->app = new \Slim\Slim();
$this->app->post("/post/create", function () {
$userId = $this->app->request->post('user');
// or
$allPostVars = $this->app->request->post();
$userId = $allPostVars['user'];
//...
});
if you dont want to use anonymous function ("It is not possible to use $this from anonymous function before PHP 5.4.0"), i think you can just do:
$this->app->post("/post/create", 'createPost');
function createPost() {
$userId = $this->app->request->post('user');
//...
}
0
$app->post('/api/v1/customers/new', function (Request $request, Response
$response) {
$data = $request->getParsedBody();
$ticket_data = [];
$ticket_data['name'] = filter_var($data['name'], FILTER_SANITIZE_STRING);
$ticket_data['age'] = filter_var($data['age'], FILTER_SANITIZE_STRING);
$response->getBody()->write(var_export($ticket_data, true));
return $response;
});
i just tried with following it worked for me.
the post body for the above API has 2 parameters i.e name and age. return the response to see the sent values.
[please follow this tutorial for better understanding][1]
[1]: https://www.slimframework.com/docs/tutorial/first-app.html

Riyaz
- 71
- 3