Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!