Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
Send array to controller by Ajax call, then controller return another View with that array in Laravel

I am passing array to controller by ajax. Controller is accessing array and return response successfully. But I want that when controller get array it return different view(suppose A.blade.php) and I can use that array in A.blade.php.

I have seen many replies like use window.location="url" in success:function(){} but it will only go to view without array.

Only purpose is to pass array to controller and controller return another view with array and i don't need of response.

AJAX function


    $(function(){
       $('#but').click(function() {
         alert("Button Click");
        ;
         $.ajax({
           type: 'get',
           url: '/suck',
           data: {arr},
   success: function( data ) {
    
    document.getElementById("p").innerHTML =data;
    
      
      },
   error: function(xhr, status, error) {
    alert(error);
    
   },
  dataType: 'text'
});
       });
    }); 

Controller

 public function getAjax(Request $req)
    {
        $input=$req->all();     
// Here I want when controller access array it return another view with array and no need of response
        return response()->json($input);    
    
    }

Routes.web.php

Route::get('/suck',[ajaxcontroller::class,'getAjax']);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Based on your comments, you could dynamically create a form and add the array you want to a hidden element. Then submit the form.

Untested code:

$(function()
    {
    $('#but').click ( 
                    function()
                        {   
                        var myArray = [];
                        var myJson = JSON.stringify(myArray); 

                        var myForm = $(document.createElement('form'));
                        $(myForm).attr("action", "/url/to/controller");
                        $(myForm).attr("method", "POST");
                        
                        var input = $("<input>").attr("type", "hidden").attr("name", "myArray").val(myJson);                       
                        
                        $(form).append($(input));
                    
                        $(form).submit();
                        }
                    );
    }
);
about 4 years ago · Juan Pablo Isaza Denunciar

0

To send an array from view to controller and from controller to other view: First create a form and use onsubmit attribute

<form id="myid" action="/go" method="post" onsubmit="submitForm(event)">
@csrf
         <input type="submit" value="submit">
</form>

Then write function for onsubmit


<script  type="text/JavaScript">
function submitForm(event){  
  var arr=["Tile","Desk","Door"];  // Array which has to pass to controller
    var i;
       for(i=0;i<arr.length;i++){ // create and append arr.length times
          var input = $("<input>").attr("type", "hidden").attr("name", "myArray["+i+"]").val(arr[i]);                       
             $(myid).append($(input));   // append to form              
    }   
      this.submit();  
  }
</script>

Routes

Route::post('/go',[subcontroller::class,'getArray']);

In Controller

class subcontroller extends Controller
{
    public function getArray(Request $req)
    {     
    $arr=$req->get('myArray');   
     return view('viewName',['set'=>$arr]);
    }
 }

In blade.view, access array as

@foreach($set as $item)
<div>{{$item}}</div>
@endforeach

It worked for me.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda