如何在 SLIM PHP 的 getParams() 中从数组输入字段获取 post 数据
how to get post data from array input fields in getParams() in SLIM PHP
我有一个表单,其中包含一些数组输入字段,例如 name[],age[],gender[]
等。
我正在尝试使用
的函数访问 slim php 中的 post 数据
$name = $request->getParam('name');
但是我没有得到任何数据。任何形式的帮助将不胜感激。提前致谢。
看起来你有大概的想法,但是看了文档之后。看来您需要为 post 数据雇用 slim 的助手。这是文档为了检索标题和描述的值而显示的示例。如下所述,filter_var() 不是必需的,但强烈建议和良好做法,以便通过删除任何可能造成伤害的特殊字符来增加额外的保护级别。
$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,如果您想阅读更多相关信息,这是示例的 link。
如果您想传递一个对象数组,您可以通过以 JSON 格式传递值来实现相同的目的。
例如:
我的示例 JSON
格式如下。
{
"news_title": "Title",
"news_description": "news_description",
"news_date": "03-12-2017",
"image_list": [
{
"imagedata": "data",
"fileName": "Imags12.png"
},
{
"imagedata": "data",
"fileName": "Imags11.png"
}
]
}
您可以阅读 slim
中的 JSON
数据,如下所述。
$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
}
});
在 postman 中,您可以将 JSON 对象作为行数据以 application/json
格式传递到正文部分。
通过使用这个概念,任何类型的复杂数据结构都可以传递到 slim 中,因为 JSON object.It 可以实现大多数数据传递目标。
您可以使用
访问 html 表单发送的表单数据
$name=$req->getParsedBodyParam("name");
$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);
}
我有一个表单,其中包含一些数组输入字段,例如 name[],age[],gender[]
等。
我正在尝试使用
$name = $request->getParam('name');
但是我没有得到任何数据。任何形式的帮助将不胜感激。提前致谢。
看起来你有大概的想法,但是看了文档之后。看来您需要为 post 数据雇用 slim 的助手。这是文档为了检索标题和描述的值而显示的示例。如下所述,filter_var() 不是必需的,但强烈建议和良好做法,以便通过删除任何可能造成伤害的特殊字符来增加额外的保护级别。
$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,如果您想阅读更多相关信息,这是示例的 link。
如果您想传递一个对象数组,您可以通过以 JSON 格式传递值来实现相同的目的。
例如:
我的示例 JSON
格式如下。
{
"news_title": "Title",
"news_description": "news_description",
"news_date": "03-12-2017",
"image_list": [
{
"imagedata": "data",
"fileName": "Imags12.png"
},
{
"imagedata": "data",
"fileName": "Imags11.png"
}
]
}
您可以阅读 slim
中的 JSON
数据,如下所述。
$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
}
});
在 postman 中,您可以将 JSON 对象作为行数据以 application/json
格式传递到正文部分。
通过使用这个概念,任何类型的复杂数据结构都可以传递到 slim 中,因为 JSON object.It 可以实现大多数数据传递目标。
您可以使用
访问 html 表单发送的表单数据$name=$req->getParsedBodyParam("name");
$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);
}