0

I have an html element inside my blade file:

<input id="check_all" onclick="toggleCheckOrders('$orderFormObject', ' $count')" name="check_all" type="checkbox" value="1">

The onclick handler is assigned a javascript function toggleCheckOrders which accepts two parameters which are objects that were passed from the controller to the blade template. When I do this, it just treats them as strings inside the javascript function, it there a way to pass them while retaining their respective data types?

0

4 Answers 4

0

You should use:

<input id="check_all" onclick="toggleCheckOrders('{{ $orderFormObject }}', ' {{ $count }}')" name="check_all" type="checkbox" value="1">

In Blade you use {{ ... }} to echo variable value

Sign up to request clarification or add additional context in comments.

6 Comments

I just tried that but it throws an error: htmlspecialchars() expects parameter 1 to be string, object given
@VinceGonzales json_encode and then parse on JS side. Laravel's {{ }} not only echos but it also escapes via htmlspecialchars as the error suggests.
So it seems, you pass here something else, do you really want to pass the whole PHP object? If yes, you can use json_encode
So it's gonna be: toggleCheckOrders('{{ json_encode($orderFormObject) }}', '{{ $count }}')" . Right?
@VinceGonzales remove the single quotes arround the {{ }}. json_encode will add quotes to the variable if necessary.
|
0

Here's a little helper I wrote for myself:

I added this in my AppServiceProvider.php:

 Blade::directive('jsvar', function ($expression) {
     return "<?php echo json_encode($expression); ?>";
 });

This will allow you to do:

<input id="check_all" onclick="toggleCheckOrders(@jsvar($orderFormObject), @jsvar($count))" name="check_all" type="checkbox" value="1">

If you find yourself passing PHP variables to JavaScript often I highly recommend this approach.

Comments

0

you can simply do

<input id="check_all" onclick="toggleCheckOrders({{json_encode($orderFormObject)}}, {{ json_encode($count)}})" name="check_all" type="checkbox" value="1">

carefull that associative array will be casted as object in JavaScript

5 Comments

How do I parse $orderFormObject in the javascript function tho? It's actually a form object passed from the controller
well, dump it and check what you need from it. it might not contain what you need anyway.
Yeah I did, but it was an empty object, so I thought there was something else that needed to be done
what are you looking for in the first parameter ?
The first parameter is actually a form object from the controller, I'm trying to pass the form object to get the elements of the form.
0

In HTML do as seen below:

<input id="check_all" onclick="jsFunction(`{{json_encode($orderFormObject)}}`, `{{ json_encode($count)}}`)" name="check_all" type="checkbox" value="1">

In javascript, do the following:

<script>
  function jsFunction(orderFormObject, count) {
      const orderFormObjectNew = JSON.parse(orderFormObject);

      console.log(orderFormObjectNew);
      console.log(count);
  }
</script>

I hope this will be helpful.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.