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

208
Vistas
How can I use an array in blade.page in javascript?

I have an array that I generated in my Controller.php page like this and I want to use it in blade.php page in javascript. I will explain the details below. I'm moving this to my blade.php page using compact('ids') .

array:11 [▼
  0 => 100485
  1 => 100496
  2 => 100497
  3 => 100498
  4 => 100499
  5 => 100500
  6 => 100502
  7 => 100504
  8 => 100482
  9 => 100486
  10 => 112995
]

Then there is an input like this on my blade.php page.

<input type="text" value="{{ request()->get('id') }}" name="id" class="form-control input-sm" placeholder="ID" required>

There is a form like this.

 <form action="" id="your_form" onsubmit="yourFunction()">
            <div class="row">
                <div class="col-lg-2">
                    <input id="coffee-submit" class="btn btn-primary mt-35" type="submit" value="detail">
                </div>
            </div>
            <br>
        </form>

yourFunction() like this

var action_src = "http://localhost:8000/admin/" + document.getElementsByName("id")[0].value + "/orders";
var your_form = document.getElementById("your_form");
your_form.action = action_src;

No problems so far and it works. What I want to do is put a control in this function. If the value entered in the input exists in $ids, the function should work. So let me give an example:

var ids = <?php echo json_encode($ids); ?>;

if (ids.includes(document.getElementsByName("id")[0].value)) {
var action_src = "http://localhost:8000/admin/" + document.getElementsByName("id")[0].value + "/orders";
    var your_form = document.getElementById("your_form");
    your_form.action = action_src; 
}

I tried as above. I tried the following, it didn't work. var ids = JSON.parse('{{ json_encode($ids) }}');

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It seems like an issue with string to integer comparison. So I added Number()

var ids = [12, 13, 14, 15];


if (ids.indexOf(Number(document.getElementsByName("id")[0].value)) > -1) {
  console.log("ok, do the ajax");

} else {
  console.log("not found");
}
<input type="text" value="14" name="id" class="form-control input-sm" placeholder="ID" required>

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