i am having a form with some array input fields like name[],age[],gender[]
etc. and i am trying to access the post data in slim php with a function using
$name = $request->getParam('name');
but iam not getting any data. Any kind of help will be appreciated. Thanks in advance.
$names = $request->getParam('name');
$genders = $request->getParam('gender');
$ages = $request->getParam('age');
$persons = array();
for($i=0;$i<count($names);$i++){
$arr['name'] = $names[$i];
$arr['gender'] = $genders[$i];
$arr['age'] = $ages[$i];
array_push($persons,$arr);
}
It looks like you have the general idea, but after looking at the documentation. It seems like you need to employ slim's helpers for the post data. This is the example that the documentation displays in order to retrieve the values of title and description. As mentioned below, the filter_var() is not necessary but it is highly recommended and good practice in order to add an extra level of protection by removing any special characters that might do harm.
$app->post('/ticket/new', function (Request $request, Response $response) {
$data = $request->getParsedBody();
$ticket_data = [];
$ticket_data['title'] = filter_var($data['title'], FILTER_SANITIZE_STRING);
$ticket_data['description'] = filter_var($data['description'], FILTER_SANITIZE_STRING);
// ...
https://www.slimframework.com/docs/tutorial/first-app.html, This is the link for the example if you would like to read more about it.
If you want to pass an array of objects you can achieve the same by passing the value in JSON format.
For example: My Sample JSON
format is as below.
{
"news_title": "Title",
"news_description": "news_description",
"news_date": "03-12-2017",
"image_list": [
{
"imagedata": "data",
"fileName": "Imags12.png"
},
{
"imagedata": "data",
"fileName": "Imags11.png"
}
]
}
You can read this JSON
data in slim
as described below.
$app->post('/create_news_json', function () use ($app) {
$json = $app->request->getBody();
$data = json_decode($json, true); // parse the JSON into an assoc. array
$news_title=$data['news_title']; // to retrieve value from news_title
$news_description=$data['news_description']; // to retrieve value from news_description
$news_date = date_format(date_create($data['news_date']),"Y-m-d"); // to
retrieve value from news_date and convert the date into Y-m-d format
$news_ImageList=$data['image_list']; //read image_list array
$arr_length=count($data['image_list']);//calculate the length of the array.
// trace each elements in image_list array as follows.
for($i=0;$i<count($news_ImageList);$i++)
{
$imagedata = $news_ImageList[$i]['imagedata']; //read image_list[].imagedata element
$filename = $news_ImageList[$i]['fileName']; //read image_list[].fileName element
}
});
In postman you can pass the JSON object as row data in the format of application/json
in body section.
By using this concept any kind of complex data structures can be passed into the slim as JSON object.It can accomplish most of you data passing goals.
You can accesss form data sent by html form with
$name=$req->getParsedBodyParam("name");