0

I need to pass HTML form fields using POST method to Slim framework in php. please help.

Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
Raja
  • 25
  • 6

3 Answers3

0

In index.php file

  1. Initialize the Function and Route url

    $app->post('/xyz/routename', 'functionname');
    
  2. 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
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