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.

Saikat Bepari
  • 121
  • 1
  • 14

4 Answers4

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.

Javier
  • 39
  • 4
  • 2
    You could edit your correct answer and explain that the "filter_var()" step is optional (yet recommended in many cases) – Benni Sep 21 '17 at 05:26
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.

Jino Shaji
  • 1,097
  • 14
  • 27
  • how do i create JSON for my form? – Saikat Bepari Sep 21 '17 at 10:33
  • Refer the [link](https://stackoverflow.com/questions/22195065/how-to-send-a-json-object-using-html-form-data) – Jino Shaji Sep 21 '17 at 10:35
  • $data = json_decode($json, true); is giving error Warning: json_decode() expects parameter 1 to be string, array given – Saikat Bepari Sep 21 '17 at 11:00
  • Just show your value in `$json` variable by displaying it using `echo $json` – Jino Shaji Sep 21 '17 at 11:18
  • Notice: Array to string conversion in C:\xampp\htdocs\etourist\classes\saikatbepari\callables\AuthenticationCallable.php on line 28 Array – Saikat Bepari Sep 21 '17 at 11:47
  • but when i use : – Saikat Bepari Sep 21 '17 at 11:48
  • $json = $request->getParsedBody(); print_r($json); – Saikat Bepari Sep 21 '17 at 11:48
  • i get this array string: Array ( [showId] => 1 [noOfTickets] => 2 [name] => Array ( [0] => Saikat Bepari [1] => Sai ) [gender] => Array ( [0] => male [1] => male ) [age] => Array ( [0] => 25 [1] => 23 ) [nationality] => Array ( [0] => Indian [1] => Indian ) [idProofType] => Array ( [0] => Indian ) [idNo] => Array ( [0] => 5555 ) [address] => eee [city] => ddddd [state] => sssss [country] => India [email] => me@saikatbepari.in [mobile] => 9476012711 [emergencyMobile] => 9476012713 ) – Saikat Bepari Sep 21 '17 at 11:49
  • How do you send this array to slim api from your form? Did you convert this array into JSON before sending it into you slim api? – Jino Shaji Sep 21 '17 at 12:09
  • 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" }); – Saikat Bepari Sep 22 '17 at 05:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155055/discussion-between-jino-shaji-and-saikat-bepari). – Jino Shaji Sep 22 '17 at 06:01
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);
}
Saikat Bepari
  • 121
  • 1
  • 14
0

You can accesss form data sent by html form with

$name=$req->getParsedBodyParam("name");
Ramy hakam
  • 522
  • 1
  • 6
  • 19