Home Ask Login Register

Developers Planet

Your answer is one click away!

Patrick J February 2016

Upgrade SLIM Framework v2 to SLIM Framework V3 ajax form data

I try to upgrade my site code to slim v3 with twig from slim v2 with smarty. I have tried some things with posting data the only thing is the formdata is not parsed and the page is reloading after submit.

The code i have using on v2 is:

$myapp->slim->post('/ajaxlogin', function() use ($myapp) {
$myapp->slim->contentType('application/json');
echo json_encode($myapp->Login($_POST['username'], $_POST['password']));

});

i have already partly changed it to:

$myapp->slim->post('/ajaxlogin', function($req, $res, $args) use ($myapp) {
$myapp->slim->contentType('application/json');
echo json_encode($myapp->Login($_POST['username'], $_POST['password']));

});

how can i get this to work again.

Answers


Patrick J February 2016

Have fixed it with the next code:

$myapp->slim->post('/ajaxlogin', function($req, $res, $args) use ($myapp) {
$data = $req->getParsedBody();
$result = json_encode($myapp->login($data['username'], $data['password']));
return $res->withStatus(200)->withHeader('Content-type', 'application/json;charset=utf-8')->write($result);

});

Post Status

Asked in February 2016
Viewed 3,684 times
Voted 4
Answered 1 times

Search




Leave an answer


Quote of the day: live life