如何从SLIM PHP中的getParams()中的数组输入字段获取发布数据

我的表单包含一些数组输入字段,如 name [],age [],gender [ ] </ code>等
和我试图使用</ p>

  $ name = $ request-&gt; getParam('name)访问苗条php中的帖子数据 '); 
</ code> </ pre>

但我没有得到任何数据。 任何形式的帮助将不胜感激。 提前致谢。 </ p>
</ div>

展开原文

原文

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.

doushi9444
doushi9444 您可以将数据作为JSON对象传递。有关详细信息,请查看以下答案。
大约 3 年之前 回复

4个回答



  $ names = $ request-&gt; getParam('name'); 
$ genders = $ request-&gt; getParam('gender');
$ ages = $ request-&gt; getParam('age');
$ persons = array();
for($ i = 0; $ i&lt; count($ names); $ i ++){
$ arr ['name'] = $ names [$ i];
$ arr ['gender'] = $ genders [$ i];
$ arr ['age'] = $ ages [$ i];
array_push($ persons,$ arr);
}
</ code> </ pre>
</ div>

展开原文

原文

$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);
}



如果要传递一个对象数组,可以通过传递JSON格式的值来实现相同的目的。</ p>

例如:
我的示例 JSON </ code>格式如下。</ p>

  {
“news_title”:“Title”,\ n“news_description”:“news_description”,
“news_date”:“03-12-2017”,
“image_list”:[
{
“imagedata”:“data”,
“fileName”:“ Imags12.png“
},
{
”“imagedata”:“data”,
“fileName”:“Imags11.png”
}

]
}
</ code> < / pre>

您可以在 slim </ code>中阅读此 JSON </ code>数据,如下所述。</ p>

   $ app-&gt; post('/ create_news_json',function()use($ app){
$ json = $ app-&gt; request-&gt; getBody();
$ data = json_decode($ json,true ); //将JSON解析为一个关联数组
$ news_title = $ data ['news_title']; //转到r 来自news_title的新价值
$ news_description = $ data ['news_description']; //从news_description
$ news_date = date_format(date_create($ data ['news_date']),“Y-m-d”)中检索值; //来自
从news_date中检索值并将日期转换为Y-m-d格式

$ news_ImageList = $ data ['image_list']; //读取image_list数组
$ arr_length = count($ data ['image_list']); //计算数组的长度。
//跟踪image_list数组中的每个元素,如下所示。
for($ i = 0; $ i&lt; count($ news_ImageList); $ i ++)
{
$ imagedata = $ news_ImageList [$ i] ['imagedata']; // read image_list [] .imagedata element
$ filename = $ news_ImageList [$ i] ['fileName']; //读取image_list [] .fileName元素
}

});
</ code> </ pre>

在邮递员中,您可以将JSON对象作为行数据传递给格式 主体部分中的 application / json </ code>。</ p>

通过使用这个概念,任何类型的复杂数据结构都可以作为JSON对象传递给slim。它可以实现最多 数据传递目标。</ p>
</ div>

展开原文

原文

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.

draxq02664
draxq02664 让我们在聊天中继续讨论。
接近 3 年之前 回复
dpnru86024
dpnru86024 var formData = JSON.stringify($ form.serializeArray()); $ .ajax({type:“POST”,url:$ form.attr('action'),data:formData,success:function(result){console.log(result);},dataType:“json”,contentType :“application / json”});
接近 3 年之前 回复
dongza1708
dongza1708 你如何从你的表单发送这个数组到slim api? 你把这个数组转换成JSON之后再将它发送到你的瘦api吗?
大约 3 年之前 回复
dtw52353
dtw52353 我得到这个数组字符串:Array([showId] => 1 [noOfTickets] => 2 [name] => Array([0] => Saikat Bepari [1] => Sai)[性别] =>数组([0 ] =>男[1] =>男性)[年龄] =>数组([0] => 25 [1] => 23)[国籍] =>数组([0] =>印度[1] =>印度 )[idProofType] =>数组([0] =>印度)[idNo] =>数组([0] => 5555)[地址] => eee [city] => ddddd [state] => sssss [country] => India [email] => me@saikatbepari.in [mobile] => 9476012711 [emergencyMobile] => 9476012713)
大约 3 年之前 回复
dongxueji2838
dongxueji2838 $ json = $ request-> getParsedBody(); 的print_r($ JSON);
大约 3 年之前 回复
dongpu2727
dongpu2727 但是当我使用时:
大约 3 年之前 回复
dtoaillwk759656786
dtoaillwk759656786 注意:第28行数组中C:\ xampp \ htdocs \ etourist \ classes \ saikatbepari \ callables \ AuthenticationCallable.php中的数组到字符串转换
大约 3 年之前 回复
douwaif22244
douwaif22244 只需使用echo $ json显示$ json变量中的值即可
大约 3 年之前 回复
duanhuan8983
duanhuan8983 $ data = json_decode($ json,true); 给出错误警告:json_decode()期望参数1是字符串,给定数组
大约 3 年之前 回复
drxnfdx798517235
drxnfdx798517235 请参阅链接
大约 3 年之前 回复
dongtan8532
dongtan8532 我如何为我的表单创建JSON?
大约 3 年之前 回复



看起来你有一般的想法,但看了一下文档。 看起来你需要为帖子数据使用slim的助手。 这是文档显示的示例,以便检索标题和描述的值。 如下所述,filter_var()不是必需的,但强烈建议和良好做法是通过删除可能有害的任何特殊字符来增加额外的保护级别。 </ p>

  $ app-&gt; post('/ ticket / new',function(Request $ request,Response $ response){
$ data = $ request-&gt; getParsedBody ();
$ ticket_data = [];
$ ticket_data ['title'] = filter_var($ data ['title'],FILTER_SANITIZE_STRING);
$ ticket_data ['description'] = filter_var($ data [' description'],FILTER_SANITIZE_STRING);
// ...
</ code> </ pre>

https://www.slimframework.com/docs/tutorial/first-app.html ,如果您想阅读,这是该示例的链接 更多关于它。</ p>
</ div>

展开原文

原文

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.

douzi9430
douzi9430 您可以编辑正确答案并解释“filter_var()”步骤是可选的(但在许多情况下仍建议使用)
大约 3 年之前 回复



您可以使用</ p>

  $ name = $访问html表单发送的表单数据 req-&gt; getParsedBody Param(“name”); \和</ code> </ pre> 
</ div>

展开原文

原文

You can accesss form data sent by html form with

$name=$req->getParsedBodyParam("name");

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐