Question

Is it possible to send an array as a parameter to a function? Searched this online but didn't help me much further. This is what I have :

In my twig I have:

<a id="exporttocsv" href="{{ path('dashboard.index', {'data': tableresults|json_encode}) }}" class="btn btn-default">Export to CSV</a>

And in my Controller:

public function exporttocsvAction(Application $app, Request $request, $data)
{
    header("Content-Type: text/csv");
    header("Content-Disposition: attachment; filename=test.csv");
    header("Pragma: no-cache");
    header("Expires: 0");

    $output = fopen("php://output", "w");

    foreach ($data as $row)
    {
        fputcsv($output, $row, ',');
    }

    fclose($output);
    exit();
}

But when I click my button, my link changes to something like this and just refreshes:

http://thelinktomypage.com/en/dashboard?data=%5B%7B%22Code%22%3A%22010%22%2C%22Name%22%3A%22Rebecca+Zygmunt%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22014%22%2C%22Name%22%3A%22Maria+Berhe%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22021%22%2C%22Name%22%3A%22Patrick+J%5Cu00f6hnk%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22030%22%2C%22Name%22%3A%22Alisha+Helena+Storr%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22033%22%2C%22Name%22%3A%22Jan+Nicolai+Kaminski%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22034%22%2C%22Name%22%3A%22Sebastian+Mantel%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22035%22%2C%22Name%22%3A%22Sabrina+Herrmann%22%2C%22PA%22%

Is it possible to send an array? Or should I do this with an ajax call and send the data?

Was it helpful?

Solution

To pass an array in a GET request check this answer how to pass an array in GET in PHP?.

You can also do that on a POST request, for instance using a form as described here Passing arrays from HTML form to PHP.

But in your case it seems the best way would be to pass it using ajax+json and then decode on the server side. Check the following answer How to pass an array using PHP & Ajax to Javascript? :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top