1

I'm with an issue for some days that I can't solve.

I should have a javascript/ajax/jQuery function in my View that creates an array and send the user to a new page using the Route "/modulos/contas_ti/gerar_protocolo"

Here is my javascript:

function check() {
    // I don't know how many numbers I will have in my array.
    // It will be dinamic. Can be [1, 2] or [1, 4, 5, 6] or anything else.
    var array = [1, 2];

    // I would like to pass 'array' in the URL below as parameter
    window.location.href = "{{URL::to('/modulos/contas_ti/gerar_protocolo')}}"
}

My Route:

// Maybe pass the array at the end of 'gerar_protocolo'? 
// Like 'gerar_protocolo[]=' ?

Route::get('/modulos/contas_ti/gerar_protocolo', 'ContasTI\ContasTIController@gerarProtocolo');

My Controller:

// How to pass the array as parameter inside ()? I also need to 
// pass the array to the new view using 'with', right? 
// Like with->('datas', $data);

public function gerarProtocolo() {
    return view('modulos.contas-ti.gerar_protocolo');
}

4 Answers 4

1

You can send it as a request parameter

function check() {
    var array = [1, 2];

    window.location.href = "{{URL::to('/modulos/contas_ti/gerar_protocolo')}}" + "?array[]=1&array[]=2";
}

Controller :

public function gerarProtocolo(Request $request) {
    $data = request('array');
    return view('modulos.contas-ti.gerar_protocolo', compact('data'));
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, Misa. Thank you! The thing is that I don't know exactly how many numbers my array will have. It will be dinamic. I added this obs to the post now.
This might help jsfiddle.net/7vaLcjxs/19 I get it from here with a modification to array parameter stackoverflow.com/questions/25736718/…
Hello, Misa! It worked, I made a few changes and it worked! Thank you so much!
0

A better way to do is, create one form with hidden variable than when you are calling you javascript check() function, store the array value in the hidden variable and than use javascript form.submit() method.

Comments

0

function check() { var array = [1, 2, n]; var myArray= encodeURIComponent(JSON.stringify(array)); window.location.href = "{{URL::to('/modulos/contas_ti/gerar_protocolo')}}" + "?array=" + myArray; }

Controller :

public function gerarProtocolo() { $data = request->json()->('array'); return view('modulos.contas-ti.gerar_protocolo', compact('data')); }

Comments

0

You can convert the array to json using JSON.stringify():

json_data = JSON.stringify(my_array);

So then you can form your url as: 'example.com/api?json_data='+json_data Therefore in your controller, simply retrieve it by calling:

$data = request()->get('json_data');
$data_array = json_decode($data, true); //converts to array
return view('modulos.contas-ti.gerar_protocolo', compact('data_array'));

In your view, the variable $data_array will be available for usage.

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.