1

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.

1
  • You can pass the data as JSON object. For more info look at the answer below. Commented Sep 21, 2017 at 5:19

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

11 Comments

how do i create JSON for my form?
Refer the link
$data = json_decode($json, true); is giving error Warning: json_decode() expects parameter 1 to be string, array given
Just show your value in $json variable by displaying it using echo $json
Notice: Array to string conversion in C:\xampp\htdocs\etourist\classes\saikatbepari\callables\AuthenticationCallable.php on line 28 Array
|
2

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.

1 Comment

You could edit your correct answer and explain that the "filter_var()" step is optional (yet recommended in many cases)
1
$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);
}

Comments

0

You can accesss form data sent by html form with

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.